Change font

image_pdfimage_print


   
  

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="Typography_Samp.Page1"
  WindowTitle="Typography Variants Sample">
  <FlowDocument Name="tf1" FontSize="18" ColumnWidth="600.0">
    <Paragraph FontSize="20" Margin="0,0,0,10">Typography Variants Sample</Paragraph>

    <Paragraph Margin="0,0,0,50">
      this is a test
    </Paragraph>

    <Paragraph>
      <StackPanel Orientation="Horizontal" Margin="10" HorizontalAlignment="Center">
        <Button Click="changeArial">Arial</Button>
        <Button Click="changePalatino">Palatino Linotype</Button>
        <Button Click="changeTimes">Times New Roman</Button>
        <Button Click="changeVerdana">Verdana</Button>
      </StackPanel>

      <LineBreak/>
      Normal <Run Typography.Variants="Superscript">superscript</Run>
      <Run Typography.Variants="Subscript">subscript</Run>
      1<Run Typography.Variants="Ordinal">st</Run>
      H<Run Typography.Variants="Inferior">2</Run>
      SO<Run Typography.Variants="Inferior">4</Run>
    </Paragraph>
    
  </FlowDocument>
</Page>

//File:Window.xaml.cs

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

namespace Typography_Samp
{

  public partial class Page1 : Page
  {
        public void changeArial(object sender, RoutedEventArgs e)
        {
            tf1.FontFamily = new FontFamily("Arial");
        }

        public void changePalatino(object sender, RoutedEventArgs e)
        {
            tf1.FontFamily = new FontFamily("Palatino Linotype");
        }

        public void changeTimes(object sender, RoutedEventArgs e)
        {
            tf1.FontFamily = new FontFamily("Times New Roman");
        }

        public void changeVerdana(object sender, RoutedEventArgs e)
        {
            tf1.FontFamily = new FontFamily("Verdana");
        }
    }
}

   
    
     


Fire an event when an element gains and loses focus.

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

   
    
     


Font Properties Moved

image_pdfimage_print


   
      

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

   
    
    
    
    
    
     


Animate TextBox Font Size

image_pdfimage_print


   
      

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

   
    
    
    
    
    
     


Event firing sequence

image_pdfimage_print


   
  
<Window 
    x:Class="TunnelingBubbling.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TunnelingBubbling">
  
    <Grid MouseLeftButtonDown="MouseDownGrid" PreviewMouseLeftButtonDown="PreviewMouseDownGrid" Width="300" Height="300">
    <Button PreviewMouseLeftButtonDown="PreviewMouseDownButton" MouseLeftButtonDown="MouseDownButton"
      Click="MyClickEvent" Name="btnGo">
      <TextBox MouseLeftButtonDown="MouseLeftButtonDown" PreviewMouseLeftButtonDown="PreviewMouseLeftButtonDown"
        Width="200" Height="30" Name="textBox1">
      </TextBox>
    </Button>
  </Grid>
</Window>
//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.Diagnostics;

namespace TunnelingBubbling
{

    public partial class Window1 : System.Windows.Window
    {

        public Window1()
        {
            InitializeComponent();
        }

        private void PreviewMouseDownGrid(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("PreviewMouseDownGrid");
        }

        private void PreviewMouseDownButton(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("PreviewMouseDownButton");
        }

        private void PreviewMouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("PreviewMouseLeftButtonDown");
        }

        private void MyClickEvent(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("MyClickEvent");
        }

        private void MouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("MouseLeftButtonDown");
        }

        private void MouseDownButton(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("MouseDownButton");
        }

        private void MouseDownGrid(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("MouseDownGrid");
        }

    }
}

   
    
     


ToolBar and event handler

image_pdfimage_print


   
  

<Window x:Class="Commands.TwoDocument"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TwoDocument" Height="300" Width="300">

  <Window.Resources>
    <CommandBinding x:Key="binding" Command="ApplicationCommands.Save" Executed="SaveCommand" CanExecute="SaveCommand_CanExecute" />
  </Window.Resources>

  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <ToolBarTray Grid.Row="1">
      <ToolBar>
        <Button Command="New">New</Button>
        <Button Command="Open">Open</Button>
        <Button Command="Save">Save</Button>
      </ToolBar>
      <ToolBar>
        <Button Command="Cut">Cut</Button>
        <Button Command="Copy">Copy</Button>
        <Button Command="Paste">Paste</Button>
      </ToolBar>
    </ToolBarTray>
    <TextBox Margin="5" Grid.Row="2" TextWrapping="Wrap" AcceptsReturn="True" TextChanged="txt_TextChanged">
      <TextBox.CommandBindings>
        <StaticResource ResourceKey="binding"></StaticResource>
      </TextBox.CommandBindings>
      
    </TextBox>

  </Grid>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Commands
{
    public partial class TwoDocument : System.Windows.Window
    {
        public TwoDocument()
        {
            InitializeComponent();
        }

        private void SaveCommand(object sender, ExecutedRoutedEventArgs e)
        {            
        }
        private void txt_TextChanged(object sender, RoutedEventArgs e)
        {
        }
        private void SaveCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = false;
        }
    }
}

   
    
     


Cancel event by setting CanExecute and Handled to false

image_pdfimage_print


   
  

<Window x:Class="Commands.NoCommandTextBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="NoCommandTextBox" Height="300" Width="300">
    <Grid>
      <TextBox Name="txt"/>
    </Grid>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Commands
{
    public partial class NoCommandTextBox : System.Windows.Window
    {
        public NoCommandTextBox()
        {
            InitializeComponent();           
        
            txt.CommandBindings.Add(new CommandBinding(ApplicationCommands.Cut, null, SuppressCommand));

        }
     
        private void SuppressCommand(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = false;
            e.Handled = true;
        }
    }
}