Event Triggers as Resource


   
       

<Window x:Class="Styles.EventTriggers"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="EventTriggers" Height="300" Width="300">
  <Window.Resources>
    <Style x:Key="BigFontButton">
      <Style.Setters>
        <Setter Property="Control.FontSize" Value="18" />
        <Setter Property="Control.FontWeight" Value="Bold" />
      </Style.Setters>
      
      <Style.Triggers>
        <EventTrigger RoutedEvent="Mouse.MouseEnter">
          <EventTrigger.Actions>
            <BeginStoryboard>
              <Storyboard>
                <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="FontSize"
                  To="22"  />
              </Storyboard>
            </BeginStoryboard>
          </EventTrigger.Actions>
        </EventTrigger>
        <EventTrigger RoutedEvent="Mouse.MouseLeave">
          <EventTrigger.Actions>
            <BeginStoryboard>
              <Storyboard>
                <DoubleAnimation
                  Duration="0:0:1"
                  Storyboard.TargetProperty="FontSize"  />
              </Storyboard>
            </BeginStoryboard>
          </EventTrigger.Actions>
        </EventTrigger>

      </Style.Triggers>
    </Style>
    
  </Window.Resources>

  <StackPanel Margin="5">
    <Button Padding="5" Margin="5" Style="{StaticResource BigFontButton}">A Customized Button</Button>
  </StackPanel>
</Window>

   
    
    
    
    
    
    
     


Reuse Font With Resources


   
       
<Window x:Class="Styles.ReuseFontWithResources"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ReuseFontWithResources" Height="368" Width="378"
    xmlns:s="clr-namespace:System;assembly=mscorlib"
    >
  <Window.Resources>    
    <FontFamily x:Key="ButtonFontFamily">Times New Roman</FontFamily>
    <s:Double x:Key="ButtonFontSize">18</s:Double>
    <FontWeight x:Key="ButtonFontWeight">Bold</FontWeight>    
  </Window.Resources>
  <StackPanel Margin="5">
    <Button Padding="5" Margin="5"
            FontFamily="{StaticResource ButtonFontFamily}"
            FontWeight="{StaticResource ButtonFontWeight}"
            FontSize="{StaticResource ButtonFontSize}" 
              >A Customized Button</Button>
  </StackPanel>
</Window>

   
    
    
    
    
    
    
     


RepeatButtons have their delay properties set to 500 milliseconds and their interval properties set to 100.


   
  



<DockPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="RepeatButtons.Pane1">
  <StackPanel>
      <RepeatButton Width="100" DockPanel.Dock="Top" 
                    Delay="500" Interval="100" 
                    Click="Increase">
        Increase
      </RepeatButton>
      <TextBlock Name="valueText" 
                 Width="100" DockPanel.Dock="Top" 
                 TextAlignment="Center" FontSize="16">
        0
      </TextBlock>

      <RepeatButton Width="100" DockPanel.Dock="Top" 
                    Delay="500" Interval="100" 
                    Click="Decrease">
        Decrease
      </RepeatButton>
    </StackPanel>
</DockPanel>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Data;

namespace RepeatButtons
{
    public partial class Pane1 : DockPanel
    {
        void Increase(object sender, RoutedEventArgs e)
        {
            Int32 Num = Convert.ToInt32(valueText.Text);

            valueText.Text = ((Num + 1).ToString());
        }

        void Decrease(object sender, RoutedEventArgs e)
        {
            Int32 Num = Convert.ToInt32(valueText.Text);

            valueText.Text = ((Num - 1).ToString());
        }
    }
}

   
    
     


Set the slider value with RepeatButton


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="100" Width="200">
    <StackPanel>
        <Slider Name="slider" Maximum="100" Minimum="0" Value="50" />
        <StackPanel Orientation="Horizontal">
            <RepeatButton Click="SliderLeft_Click" Content="Click Me" 
                          Height="23" Margin="10" Width="70" 
                          ToolTip="Click to move slider left" />
            <RepeatButton Click="SliderRight_Click" ClickMode="Hover" 
                          Content="Touch Me" Height="23" Margin="10" 
                          ToolTip="Hover to move slider right" Width="70" />
        </StackPanel>
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Windows;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void SliderLeft_Click(object sender, RoutedEventArgs e)
        {
            slider.Value -= 1;
        }

        private void SliderRight_Click(object sender, RoutedEventArgs e)
        {
            slider.Value += 1;
        }
    }
}

   
    
     


Use outter resource or inner resource

   
       

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

  <Window.Resources>
    <ImageBrush x:Key="TileBrush" TileMode="Tile"
                ViewportUnits="Absolute" Viewport="0 0 32 32"
                ImageSource="c:image.jpg" Opacity="0.3"></ImageBrush>
  </Window.Resources>
  <StackPanel Margin="5">
    
    <Button Background="{StaticResource TileBrush}" Padding="5"
            FontWeight="Bold" FontSize="14" Margin="5"
              >A Tiled Button</Button>

    <Button Padding="5" Margin="5" FontWeight="Bold" FontSize="14">
      <Button.Resources>
        <ImageBrush x:Key="TileBrush" TileMode="Tile"
                    ViewportUnits="Absolute" Viewport="0 0 10 10"
                    ImageSource="cimage.jpg" Opacity="0.3"></ImageBrush>
      </Button.Resources>
      <Button.Background><StaticResource ResourceKey="TileBrush" /></Button.Background>
      <Button.Content>Another Tiled Button</Button.Content>
    </Button>

  </StackPanel>

</Window>

   
    
    
    
    
    
    
     


Use outter resource or inner resource

   
       

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

  <Window.Resources>
    <ImageBrush x:Key="TileBrush" TileMode="Tile"
                ViewportUnits="Absolute" Viewport="0 0 32 32"
                ImageSource="c:image.jpg" Opacity="0.3"></ImageBrush>
  </Window.Resources>
  <StackPanel Margin="5">
    
    <Button Background="{StaticResource TileBrush}" Padding="5"
            FontWeight="Bold" FontSize="14" Margin="5"
              >A Tiled Button</Button>

    <Button Padding="5" Margin="5" FontWeight="Bold" FontSize="14">
      <Button.Resources>
        <ImageBrush x:Key="TileBrush" TileMode="Tile"
                    ViewportUnits="Absolute" Viewport="0 0 10 10"
                    ImageSource="cimage.jpg" Opacity="0.3"></ImageBrush>
      </Button.Resources>
      <Button.Background><StaticResource ResourceKey="TileBrush" /></Button.Background>
      <Button.Content>Another Tiled Button</Button.Content>
    </Button>

  </StackPanel>

</Window>

   
    
    
    
    
    
    
     


The ScaleX and ScaleY properties of this ScaleTransform are each animated from 0 to 1


   
  
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="Microsoft.Samples.Graphics.Transforms.AnimatedScaleTransformExample"
  WindowTitle="Animated ScaleTransform Example" 
  Background="White">
  
    <StackPanel Margin="10">
        <Canvas Width="250" Height="250">
          <Rectangle
            Height="50" Width="50" Fill="#CCCCCCFF" Stroke="Blue" StrokeThickness="2"
            Canvas.Left="100" Canvas.Top="100">
            <Rectangle.RenderTransform>
              <ScaleTransform x:Name="MyAnimatedScaleTransform" CenterX="25" CenterY="25" ScaleX="1" ScaleY="1" />
            </Rectangle.RenderTransform>
          </Rectangle>
          <Rectangle Height="50" Width="50" Stroke="#99000000"
            StrokeDashArray="4,1" StrokeThickness="2"
            Canvas.Left="100" Canvas.Top="100" />            
        </Canvas>
      <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"
        Margin="10">
        <Button Name="startButton" Margin="0,0,2,0">Start</Button>
        <Button Name="stopButton">Stop</Button>
        <StackPanel.Triggers>
          <EventTrigger SourceName="startButton" RoutedEvent="Button.Click">
            <BeginStoryboard Name="myBeginStoryboard">
              <Storyboard>
                <DoubleAnimation 
                  Storyboard.TargetName="MyAnimatedScaleTransform" 
                  Storyboard.TargetProperty="ScaleX" 
                  From="0" To="3" Duration="0:0:2" />
                <DoubleAnimation 
                  Storyboard.TargetName="MyAnimatedScaleTransform" 
                  Storyboard.TargetProperty="ScaleY" 
                  From="0" To="3" Duration="0:0:2" />              
              </Storyboard>
            </BeginStoryboard>
          </EventTrigger>
          <EventTrigger SourceName="stopButton" RoutedEvent="Button.Click">
            <StopStoryboard BeginStoryboardName="myBeginStoryboard" />
          </EventTrigger>          
        </StackPanel.Triggers>
      </StackPanel>  
    </StackPanel>
</Page>