Monday, August 11, 2014

Create a simple application to change the font of text in a RichTextBox control by using controls in a toolbar.

Sample code:

XAML


<Window x:Class="SimpleApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <DockPanel>
        <ToolBar DockPanel.Dock="Top" Height="26" Name="toolBar1" Width="276">
            <Button Margin="0,0,0,-0.2" VerticalAlignment="Top" Click="Button_Click_1">Bold
             </Button>
            <Button Click="Button_Click_2">Italic</Button>
            <Slider Name="Slider1" Minimum="2" Maximum="72" Width="100" 
             ValueChanged="Slider1_ValueChanged"/>
        </ToolBar>
        <Grid Name="grid1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="5"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
 
            <ListBox Grid.Column="0" Name="listBox1" 
            SelectionChanged="listBox1_SelectionChanged"></ListBox>
            <GridSplitter Name="gridSplitter1" Margin="0" Width="5"
            Grid.Column="1" HorizontalAlignment="Left"/>
            <RichTextBox Grid.Column="2" Name="richTextBox1"/>
        </Grid>       
    </DockPanel>
</Window>



Event handling codes


using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
 
namespace SimpleApp
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
      foreach (FontFamily F in Fonts.SystemFontFamilies)
      {
        ListBoxItem l = new ListBoxItem();
        l.Content = F.ToString();
        l.FontFamily = F;
        listBox1.Items.Add(l);
      }
    }
 
    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
      richTextBox1.Selection.ApplyPropertyValue(
        FontWeightProperty,
        FontWeights.Bold);
    }
 
    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
      richTextBox1.Selection.ApplyPropertyValue(
        FontStyleProperty,
        FontStyles.Italic);
    }
 
    private void Slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double>)
    {
      try
      {
        richTextBox1.Selection.ApplyPropertyValue(FontSizeProperty,
        Slider1.Value.ToString());
      }
      catch { }
    }
 
    private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      richTextBox1.Selection.ApplyPropertyValue(
        FontFamilyProperty,
        ((ListBoxItem)listBox1.SelectedItem).FontFamily);
    }
  }
}



Canvas control

The most basic layout panel in WPF. All contained controls are positioned on the basis of four
attached properties: Canvas.Top, Canvas.Bottom, Canvas.Right, and Canvas.Left. The coordinates can be specified relative to any side of the panel.

The panel is typically used to group 2D graphic elements together and not to layout user interface elements.

By default, controls declared later in the XAML are shown on top of controls declared earlier in the XAML. However, you can set the Z-order (that is, which control appears on top) manually by setting the Canvas.ZIndex attached property. Canvas.ZIndex takes an arbitrary integer value. Controls with a higher Canvas.ZIndex value always appear on top of controls with a lower Canvas.ZIndex value. An example is shown here:

<Button Canvas.ZIndex="12">This one is on top</Button>
<Button Canvas.ZIndex="5">This one is on the bottom</Button>






Wrap panel control in C# WPF

Controls are wrapped like text is wrapped in a text editor like Notepad.
Usage : A typical use for this layout panel is to provide automatic layout for a related set of controls that might be re sized frequently, such as those in a toolbar.

The WrapPanel is often used with items of same size, but it is not a requirement.



Monday, July 28, 2014

UniformGrid Control

This has a different behavior from the Grid control. Grid cells are always the same size. The UniformGrid control typically is not used for designing entire user interfaces, but it can be useful for quickly creating layouts that require a grid of uniform size, such as a checkerboard or the buttons on a calculator.

If you set only the number of rows, additional columns will be added to accommodate new
controls. Likewise, if you set only the number of columns, additional rows will be added.

 <Grid>
  <UniformGrid Rows="2" Name="uniformGrid1" >
            <Button Content="my1"></Button>
            <Button Content="my1"></Button>
            <Button Content="my1"></Button>
            <Button Content="my1"></Button>
            <Button Content="my1"></Button>
  </UniformGrid>
 </Grid>





<Grid>
<UniformGrid Columns ="2" Name="uniformGr1" >
            <Button Content="my1"></Button>
            <Button Content="my1"></Button>
            <Button Content="my1"></Button>
            <Button Content="my1"></Button>
            <Button Content="my1"></Button>
</UniformGrid>
</Grid>

Grid Splitter

Grid Splitter is a control provided by WPF. The GridSplitter cannot be used in isolation. It should be placed within a cell of a Grid. It should be configured to appear as a bar between two rows or two columns.

User can drag the bar with the mouse, or adjust its position using the keyboard, resizing the two columns or rows between which the GridSplitter appears.


Using a single GridSplitter is good for separating two areas that may each hold more information that can be seen at once. Adding multiple GridSplitters to a single Grid allows for flexible, resizable layouts such as those seen in Visual Studio, Microsoft Outlook or Windows Explorer.

Very useful articles



Wednesday, July 23, 2014

Alignment, Margin and Padding

Most of the time, we are experiencing the difficulty to distinguish between Margin and Padding properties in WPF applications. Following is a good example to refer,



Refer the following link,

http://msdn.microsoft.com/en-us/library/ms751709(v=vs.110).aspx