Programmatically change the FontFamily property of a TextBlock element.

image_pdfimage_print


   
  


<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="FontSizeConverter_Csharp.Window1"
    Title="FontSizeConverter Sample">
<StackPanel>
        <TextBlock Name="text1" TextWrapping="Wrap" Height="300" Width="500">
            this is a test
        </TextBlock>
    <ListBox Grid.Column="1" Grid.Row="1" VerticalAlignment="Top" Height="50" Margin="10,0,0,0" SelectionChanged="changeSize">
        <ListBoxItem>5</ListBoxItem>
        <ListBoxItem>8</ListBoxItem>
        <ListBoxItem>11</ListBoxItem>
        <ListBoxItem>14</ListBoxItem>
        <ListBoxItem>17</ListBoxItem>
        <ListBoxItem>20</ListBoxItem>
        <ListBoxItem>23</ListBoxItem>
        <ListBoxItem>26</ListBoxItem>
        <ListBoxItem>29</ListBoxItem>
        <ListBoxItem>32</ListBoxItem>
    </ListBox>
    <ListBox Grid.Column="3" Grid.Row="1" VerticalAlignment="Top" Height="50" Margin="10,0,0,0" SelectionChanged="changeFamily">
      <ListBoxItem>Arial</ListBoxItem>
      <ListBoxItem>Courier New</ListBoxItem>
      <ListBoxItem>Tahoma</ListBoxItem>
      <ListBoxItem>Times New Roman</ListBoxItem>
      <ListBoxItem>Verdana</ListBoxItem>
    </ListBox>      
   
  </StackPanel>        
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Windows.Media;

namespace FontSizeConverter_Csharp
{
  public partial class Window1 : Window
  {
        public void changeSize(object sender, SelectionChangedEventArgs args)
        {
      ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
      FontSizeConverter myFontSizeConverter = new FontSizeConverter();
      text1.FontSize = (Double)myFontSizeConverter.ConvertFromString(li.Content.ToString());
        }

    public void changeFamily(object sender, SelectionChangedEventArgs args)
    {
      ListBoxItem li2 = ((sender as ListBox).SelectedItem as ListBoxItem);
            text1.FontFamily = new System.Windows.Media.FontFamily(li2.Content.ToString());
        }
  }
}

   
    
     


Font Properties Sample

image_pdfimage_print


   
  

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
    x:Class="WpfApplication1.Window1"
    Title="Font Properties C# Sample">

  <StackPanel Background="Honeydew">
    <TextBlock FontWeight="Bold" FontSize="14">FontFamly:</TextBlock>
    <StackPanel Orientation="Horizontal" Margin="0,0,0,10">
      <Button Click="OnClick1" Background="Silver" Margin="3">Arial</Button>
      <Button Click="OnClick2" Background="Silver" Margin="3">Courier New</Button>
      <Button Click="OnClick3" Background="Silver" Margin="3">Tahoma</Button>
      <Button Click="OnClick4" Background="Silver" Margin="3">Times New Roman</Button>
      <Button Click="OnClick5" Background="Silver" Margin="3">Verdana</Button>
    </StackPanel>

    <TextBlock FontWeight="Bold" FontSize="14">Foreground Color:</TextBlock>
    <StackPanel Orientation="Horizontal" Margin="0,0,0,10">
      <Button Click="OnClick11" Foreground="White" Background="Black" Margin="3">Black</Button>
      <Button Click="OnClick12" Background="Blue" Margin="3">Blue</Button>
      <Button Click="OnClick13" Background="Green" Margin="3">Green</Button>
      <Button Click="OnClick14" Background="Red" Margin="3">Red</Button>
      <Button Click="OnClick15" Background="Yellow" Margin="3">Yellow</Button>
    </StackPanel>


    <TextBlock FontWeight="Bold" FontSize="14" VerticalAlignment="Center">FontSize:</TextBlock>
    <StackPanel Orientation="Horizontal" Margin="0,0,0,10">
      <RadioButton Click="OnClick6" Margin="3">8 point</RadioButton>
      <RadioButton Click="OnClick7" Margin="3">10 point</RadioButton>
      <RadioButton Click="OnClick8" Margin="3">12 point</RadioButton>
      <RadioButton Click="OnClick9" Margin="3">14 point</RadioButton>
      <RadioButton Click="OnClick10" Margin="3">16 point</RadioButton>
    </StackPanel>

    <TextBlock FontSize="12" Name="txt1">The FontFamily is set to Arial.</TextBlock>
    <TextBlock FontSize="12" Name="txt3">The FontSize is set to 12 point.</TextBlock>
    <TextBlock FontSize="12" Name="txt4" Margin="0,0,0,15">The Foreground color is set to Black.</TextBlock>

    <FlowDocumentReader>
      <FlowDocument Name="txt2" FontFamily="Arial" FontSize="12" Foreground="Black">
        <Paragraph>
          this is a test
        </Paragraph>
      </FlowDocument>
    </FlowDocumentReader>
  </StackPanel>
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace WpfApplication1
{

  public partial class Window1 : Window
  {
        public void OnClick1(object sender, RoutedEventArgs e)
        {
            txt1.Text = "The FontFamily is set to Arial.";
            txt2.FontFamily = new FontFamily("Arial");
        }

        public void OnClick2(object sender, RoutedEventArgs e)
        {
            txt1.Text = "The FontFamily is set to Courier new.";
            txt2.FontFamily = new FontFamily("Courier new");
        }

        public void OnClick3(object sender, RoutedEventArgs e)
        {
            txt1.Text = "The FontFamily is set to Tahoma.";
            txt2.FontFamily = new FontFamily("Tahoma");
        }

        public void OnClick4(object sender, RoutedEventArgs e)
        {
            txt1.Text = "The FontFamily is set to Times new Roman.";
            txt2.FontFamily = new FontFamily("Times new Roman");
        }

        public void OnClick5(object sender, RoutedEventArgs e)
        {
            txt1.Text = "The FontFamily is set to Verdana.";
            txt2.FontFamily = new FontFamily("Verdana");
        }

        public void OnClick6(object sender, RoutedEventArgs e)
        {
            txt3.Text = "The FontSize is set to 8 point.";
            txt2.FontSize = 8;
        }

        public void OnClick7(object sender, RoutedEventArgs e)
        {
            txt3.Text = "The FontSize is set to 10 point.";
            txt2.FontSize = 10;
        }

        public void OnClick8(object sender, RoutedEventArgs e)
        {
            txt3.Text = "The FontSize is set to 12 point.";
            txt2.FontSize = 12;
        }

        public void OnClick9(object sender, RoutedEventArgs e)
        {
            txt3.Text = "The FontSize is set to 14 point.";
            txt2.FontSize = 14;
        }

        public void OnClick10(object sender, RoutedEventArgs e)
        {
            txt3.Text = "The FontSize is set to 16 point.";
            txt2.FontSize = 16;
        }

        public void OnClick11(object sender, RoutedEventArgs e)
        {
            txt4.Text = "The Foreground color is set to Black.";
            txt2.Foreground = Brushes.Black;
        }

        public void OnClick12(object sender, RoutedEventArgs e)
        {
            txt4.Text = "The Foreground color is set to Blue.";
            txt2.Foreground = Brushes.Blue;
        }

        public void OnClick13(object sender, RoutedEventArgs e)
        {
            txt4.Text = "The Foreground color is set to Green.";
            txt2.Foreground = Brushes.Green;
        }

        public void OnClick14(object sender, RoutedEventArgs e)
        {
            txt4.Text = "The Foreground color is set to Red.";
            txt2.Foreground = Brushes.Red;
        }

        public void OnClick15(object sender, RoutedEventArgs e)
        {
            txt4.Text = "The Foreground color is set to Yellow.";
            txt2.Foreground = Brushes.Yellow;
        }
    }
}

   
    
     


Display a notification icon in the system tray.

image_pdfimage_print


   
  


<Window x:Class="NotificationIconSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="NotificationIconSample" Height="300" Width="300">
    <Grid>
      <Button Click="click">Click</Button>
    </Grid>
</Window>

//File:Window.xaml.cs
namespace NotificationIconSample
{
    using System;
    using System.Windows;
    using System.Windows.Forms; 
    using System.Drawing;
    public partial class MainWindow : Window
    {
        NotifyIcon notifyIcon;

        public MainWindow()
        {
            InitializeComponent();
        }

        void click(object sender, RoutedEventArgs e)
        {
            this.notifyIcon = new NotifyIcon();
            this.notifyIcon.BalloonTipText = "Hello, NotifyIcon!";
            this.notifyIcon.Text = "Hello, NotifyIcon!";
            this.notifyIcon.Icon = new System.Drawing.Icon("NotifyIcon.ico");
            this.notifyIcon.Visible = true;
            this.notifyIcon.ShowBalloonTip(1000);
        }
    }
}

   
    
     


Add the PropertyGrid to the host, and the host to the WPF Grid

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Hosting a Windows Forms Property Grid in WPF"
  Loaded="Window_Loaded">
    <Grid Name="grid"/>
</Window>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.Integration.WindowsFormsHost host =
                new System.Windows.Forms.Integration.WindowsFormsHost();
            System.Windows.Forms.PropertyGrid propertyGrid =
                new System.Windows.Forms.PropertyGrid();

            host.Child = propertyGrid;
            grid.Children.Add(host);
            
            
            propertyGrid.SelectedObject = this;
        }
    }
}

   
    
     


Use the IsSharedSizeScope attached property of the Grid element

image_pdfimage_print


   
  

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication1.Window1"
    Title="Grid IsSharedSizeScope Sample">
    <Border BorderBrush="Black" Background="White" BorderThickness="2">

  <DockPanel Name="dp1" Grid.IsSharedSizeScope="False" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10">
    <StackPanel Orientation="Horizontal" DockPanel.Dock="Top">  
        <Button Click="setTrue" Margin="0,0,10,10">Set IsSharedSizeScope="True"</Button>
        <Button Click="setFalse" Margin="0,0,10,10">Set IsSharedSizeScope="False"</Button>
    </StackPanel> 

    <StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
 
    <Grid ShowGridLines="True" Margin="0,0,10,0">
      <Grid.ColumnDefinitions>
        <ColumnDefinition SharedSizeGroup="FirstColumn"/>
        <ColumnDefinition SharedSizeGroup="SecondColumn"/>
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition Height="Auto" SharedSizeGroup="FirstRow"/>
      </Grid.RowDefinitions>
        <Rectangle Fill="Silver" Grid.Column="0" Grid.Row="0" Width="200" Height="100"/>
        <Rectangle Fill="Blue" Grid.Column="1" Grid.Row="0" Width="150" Height="100"/>

        <TextBlock Grid.Column="0" Grid.Row="0" FontWeight="Bold">First Column</TextBlock>
        <TextBlock Grid.Column="1" Grid.Row="0" FontWeight="Bold">Second Column</TextBlock>
    </Grid>

    <Grid ShowGridLines="True">
      <Grid.ColumnDefinitions>
        <ColumnDefinition SharedSizeGroup="FirstColumn"/>
        <ColumnDefinition SharedSizeGroup="SecondColumn"/>
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>        
        <RowDefinition Height="Auto" SharedSizeGroup="FirstRow"/>
      </Grid.RowDefinitions>

        <Rectangle Fill="Silver" Grid.Column="0" Grid.Row="0"/>
        <Rectangle Fill="Blue" Grid.Column="1" Grid.Row="0"/>
 
        <TextBlock Grid.Column="0" Grid.Row="0" FontWeight="Bold">First Column</TextBlock>
        <TextBlock Grid.Column="1" Grid.Row="0" FontWeight="Bold">Second Column</TextBlock>
    </Grid>
    
    </StackPanel>

  </DockPanel>
  </Border>  
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
  public partial class Window1 : Window
  {
        public void setTrue(object sender, System.Windows.RoutedEventArgs e)
        {
            Grid.SetIsSharedSizeScope(dp1, true);
            Console.WriteLine(Grid.GetIsSharedSizeScope(dp1).ToString());
        }

        public void setFalse(object sender, System.Windows.RoutedEventArgs e)
        {
            Grid.SetIsSharedSizeScope(dp1, false);
            Console.WriteLine(Grid.GetIsSharedSizeScope(dp1).ToString());
        }
  }
}

   
    
     


Use Frame to Load another Xaml file

image_pdfimage_print
   
    

<Window x:Class="SimpleStyles.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="SimpleStyles"
  Background="#F8F8F8">
  <ScrollViewer>
    <WrapPanel>
      <HeaderedItemsControl Header="Frame">
        <StackPanel>
          <Frame Source="page1.xaml" Width="200" Height="200"/>
        </StackPanel>
      </HeaderedItemsControl>
   
    </WrapPanel>
  </ScrollViewer>
</Window>