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

   
    
     


Activated and Deactivated events monitors the activation status of an application.

image_pdfimage_print
   
  

<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="ActivationSample.App"
    StartupUri="MainWindow.xaml"
    Startup="App_Startup" 
    Activated="App_Activated" 
    Deactivated="App_Deactivated"
    Exit="App_Exit">
</Application>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Threading;

namespace ActivationSample
{
    public partial class App : System.Windows.Application
    {
        void App_Startup(object sender, StartupEventArgs e)
        {
        }
        void App_Activated(object sender, EventArgs e)
        {
            Console.WriteLine("App_Activated");
        }
        void App_Deactivated(object sender, EventArgs e)
        {
            Console.WriteLine("Deactivated");
        }
        void App_Exit(object sender, ExitEventArgs e)
        {
            Console.WriteLine("Exit");
        }
    }
}

   
    
     


Event sender, event source and event original source

image_pdfimage_print


   
  

<Window x:Class="RoutedEvents.BubbledLabelClick"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="BubbledLabelClick" Height="360" Width="330" MouseUp="SomethingClicked" >
    <StackPanel MouseUp="SomethingClicked">
      <Label Margin="5" BorderThickness="1" MouseUp="SomethingClicked" HorizontalAlignment="Left" >
        <StackPanel MouseUp="SomethingClicked" >
          <TextBlock Margin="3" MouseUp="SomethingClicked">label</TextBlock>
          <Image Source="c:image.jpg" Stretch="None" MouseUp="SomethingClicked" />
          <TextBlock Margin="3" MouseUp="SomethingClicked">test</TextBlock>
        </StackPanel>
      </Label>
      <ListBox Margin="5" Name="lstMessages" Grid.Row="1"></ListBox>
      <CheckBox Margin="5" Name="chkHandle">Handle first event</CheckBox>
      <Button Click="cmdClear_Click" HorizontalAlignment="Right" Margin="5" Padding="3">Clear List</Button>
    </StackPanel>
</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 RoutedEvents
{
    public partial class BubbledLabelClick : System.Windows.Window
    {

        public BubbledLabelClick()
        {
            InitializeComponent();
        }

        protected int eventCounter = 0;

        private void SomethingClicked(object sender, RoutedEventArgs e)
        {            
            eventCounter++;
            string message = "#" + eventCounter.ToString() + ":
" + 
                " Sender: " + sender.ToString() + "
" +
                " Source: " + e.Source + "
" +
                " Original Source: " + e.OriginalSource;
            lstMessages.Items.Add(message);
            e.Handled = (bool)chkHandle.IsChecked;            
        }

        private void cmdClear_Click(object sender, RoutedEventArgs e)
        {            
            eventCounter = 0;
            lstMessages.Items.Clear();
        }
    }
}

   
    
     


Use EventSetter to add mouse event handler

image_pdfimage_print


   
  


<Window x:Class="Styles.EventSetter"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="EventSetter" Height="300" Width="300">
  <Window.Resources>
    <Style x:Key="MouseOverHighlightStyle">
      <Setter Property="TextBlock.Padding" Value="5"/>
      <EventSetter Event="FrameworkElement.MouseEnter" Handler="element_MouseEnter" />
      <EventSetter Event="FrameworkElement.MouseLeave" Handler="element_MouseLeave" />
    </Style>
  </Window.Resources>
  
  <StackPanel>
    <TextBlock Style="{StaticResource MouseOverHighlightStyle}">Hover over me.</TextBlock>
    <TextBlock Padding="5">asdf</TextBlock>
    <TextBlock Style="{StaticResource MouseOverHighlightStyle}">Hover over me.</TextBlock>
  </StackPanel>
</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 Styles
{
    public partial class EventSetter : System.Windows.Window
    {
        public EventSetter()
        {
            InitializeComponent();
        }
        private void element_MouseEnter(object sender, MouseEventArgs e)
        {
            ((TextBlock)sender).Background = new SolidColorBrush(Colors.LightGoldenrodYellow);
        }
        private void element_MouseLeave(object sender, MouseEventArgs e)
        {
            ((TextBlock)sender).Background = null;
        }
    }
}