Font Properties Sample


   
  

<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;
        }
    }
}

   
    
     


Programmatically change the FontFamily property of a TextBlock element.


   
  


<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 Viewer


   
       
<Window x:Class="FontViewer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Teach Yourself WPF: Font Viewer"
        Height="480"
        Width="640">
    <DockPanel Margin="8">

        <ListBox x:Name="FontList"
                 DockPanel.Dock="Left"
                 ItemsSource="{x:Static Fonts.SystemFontFamilies}"
                 Width="160" />
        <TextBox x:Name="SampleText"
                 DockPanel.Dock="Bottom"
                 MinLines="4"
                 Margin="8 0"
                 TextWrapping="Wrap">
            <TextBox.ToolTip>
                <TextBlock>
                    <Italic Foreground="Red">Instructions: </Italic> Type here to change the preview text.
                </TextBlock>
            </TextBox.ToolTip>
            The quick brown fox jumps over the lazy dog.
        </TextBox>
        <StackPanel Margin="8 0 8 8">
            <TextBlock Text="{Binding ElementName=SampleText, Path=Text}"
                       FontFamily="{Binding ElementName=FontList,Path=SelectedItem}"
                       FontSize="10"
                       TextWrapping="Wrap"
                       Margin="0 0 0 4" />
            <TextBlock Text="{Binding ElementName=SampleText, Path=Text}"
                       FontFamily="{Binding ElementName=FontList,Path=SelectedItem}"
                       FontSize="16"
                       TextWrapping="Wrap"
                       Margin="0 0 0 4" />
            <TextBlock Text="{Binding ElementName=SampleText, Path=Text}"
                       FontFamily="{Binding ElementName=FontList,Path=SelectedItem}"
                       FontSize="24"
                       TextWrapping="Wrap"
                       Margin="0 0 0 4" />
            <TextBlock Text="{Binding ElementName=SampleText, Path=Text}"
                       FontFamily="{Binding ElementName=FontList,Path=SelectedItem}"
                       FontSize="32"
                       TextWrapping="Wrap" />
        </StackPanel>
    </DockPanel>
</Window>

   
    
    
    
    
    
    
     


Create FishEye Effect Buttons by changing the Button font size


   
       

<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <StackPanel.Resources>
        <Style TargetType="{x:Type Button}">
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="FontSize" Value="12" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation 
                                    Storyboard.TargetProperty="FontSize"
                                    To="36" Duration="0:0:1" />
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>

                    <Trigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation 
                                    Storyboard.TargetProperty="FontSize"
                                    To="12" Duration="0:0:0.25" />
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.ExitActions>
                </Trigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Resources>

    <Button>Button No. 1</Button>
    <Button>Button No. 2</Button>
    <Button>Button No. 3</Button>
</StackPanel>

   
    
    
    
    
    
    
     


Animate TextBox Font Size


   
      

<Window x:Class="XamlOnly"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="XamlOnly"
    Height="300" Width="300">
    <Grid>
      <StackPanel>
            <TextBlock Name="textBlock" Margin="5"
                TextAlignment="Center" Height="30"
                Text="{Binding ElementName=textBox,Path=Text}" />
            <TextBox Name="textBox" Margin="5" Width="200"
                TextAlignment="Center" Text="Hello, WPF!" />
            <Button Margin="5" Width="200" Content="Change Text Size">
                <Button.Triggers>


                    <EventTrigger RoutedEvent="Button.Click">
                        <BeginStoryboard>
                            <Storyboard>


                                <DoubleAnimation
                                    Storyboard.TargetName="textBlock"
                                    Storyboard.TargetProperty="FontSize" From="11" To="24"
                                    Duration="0:0:0.2" />


                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Button.Triggers>
            </Button>

        </StackPanel>
    </Grid>
</Window>

   
    
    
    
    
    
     


Font Properties Moved


   
      

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  Title="About WPF" SizeToContent="WidthAndHeight"
  Background="OrangeRed">
  <StackPanel>
    <Label FontWeight="Bold" FontSize="20" Foreground="White">
      WPF Version
    </Label>
    <Label>2010</Label>
    <Label>Installed Dll:</Label>
    <ListBox>
      <ListBoxItem>1</ListBoxItem>
      <ListBoxItem>2</ListBoxItem>
    </ListBox>
    <StackPanel TextElement.FontSize="30" TextElement.FontStyle="Italic"
      Orientation="Horizontal" HorizontalAlignment="Center">
      <Button MinWidth="75" Margin="10">Help</Button>
      <Button MinWidth="75" Margin="10">OK</Button>
    </StackPanel>
    <StatusBar>You have successfully registered this product.</StatusBar>
  </StackPanel>
</Window>

   
    
    
    
    
    
     


Fire an event when an element gains and loses focus.


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:custom="clr-namespace:WpfApplication1"  
    Title="gotfocusLostfocusEffectUsingEvent" Height="400" Width="450"
    >
  <StackPanel>
    <StackPanel.Resources>
      <Style TargetType="{x:Type Button}">
        <Setter Property="Height" Value="20"/>
        <Setter Property="Width" Value="250"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
      </Style>
    </StackPanel.Resources>
    <Button
        GotFocus="OnGotFocusHandler"
        LostFocus="OnLostFocusHandler">Click Or Tab To Give Keyboard Focus</Button>
    <Button
        GotFocus="OnGotFocusHandler"
        LostFocus="OnLostFocusHandler">Click Or Tab To Give Keyborad Focus</Button>
  </StackPanel>
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void OnGotFocusHandler(object sender, RoutedEventArgs e)
        {
            Button tb = e.Source as Button;
            tb.Background = Brushes.Red;
        }
        private void OnLostFocusHandler(object sender, RoutedEventArgs e)
        {
            Button tb = e.Source as Button;
            tb.Background = Brushes.White;
        }
    }
}