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.