DispatcherTimer set up


   
  


<Window x:Class="_360Timer.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Concentric Rings" Width="910" Height="512">
  <Canvas Name="MainCanvas" Background="#FFE0E0E0"/>
</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;
using System.Windows.Media.Animation;


namespace _360Timer
{

    public partial class Window1 : Window
    {
        System.Windows.Threading.DispatcherTimer frameTimer;

        public Window1()
        {
            InitializeComponent();

            frameTimer = new System.Windows.Threading.DispatcherTimer();
            frameTimer.Tick += OnFrame;
            frameTimer.Interval = TimeSpan.FromSeconds(1.0 / 60.0);
            frameTimer.Start();

            this.Show();

        }


        private void OnFrame(object sender, EventArgs e)
        {
        }

    }
}

   
    
     


ToolBar.ButtonStyleKey


   
    


<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Background="White">
  <Window.Resources>
    <Style x:Key="{x:Static ToolBar.SeparatorStyleKey}" TargetType="Separator">
      <Setter Property="Background" Value="DarkBlue"/>
      <Setter Property="Width" Value="2"/>
    </Style>

    <Style x:Key="{x:Static ToolBar.ButtonStyleKey}" TargetType="Button">
      <Setter Property="Foreground" Value="Blue"/>
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="HorizontalAlignment" Value="Center"/>
      <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>

    <Style x:Key="{x:Static ToolBar.CheckBoxStyleKey}" TargetType="CheckBox">
      <Setter Property="Foreground" Value="DarkSlateBlue"/>
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="HorizontalAlignment" Value="Center"/>
      <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>
    
    <Style x:Key="{x:Static ToolBar.MenuStyleKey}" TargetType="Menu">
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="FontStyle" Value="Italic"/>
      <Setter Property="FontWeight" Value="Bold"/>
      <Setter Property="Background" Value="LightSteelBlue"/>
    </Style>
    
    <Style x:Key="{x:Static ToolBar.RadioButtonStyleKey}" TargetType="RadioButton">
      <Setter Property="Background" Value="LightSteelBlue"/>
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="HorizontalAlignment" Value="Center"/>
      <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>
   
    <Style x:Key="{x:Static ToolBar.TextBoxStyleKey}" TargetType="TextBox">
      <Setter Property="Background" Value="DarkBlue"/>
      <Setter Property="Foreground" Value="White"/>
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="FontStyle" Value="Italic"/>
      <Setter Property="HorizontalAlignment" Value="Center"/>
      <Setter Property="VerticalAlignment" Value="Center"/>
      <Setter Property="Width" Value="75"/>
    </Style>
    
    <Style x:Key="{x:Static ToolBar.ComboBoxStyleKey}" TargetType="ComboBox">
      <Setter Property="Background" Value="LightSteelBlue"/>
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="MinWidth" Value="60"/>
      <Setter Property="HorizontalAlignment" Value="Center"/>
      <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>
    
    <Style TargetType="Separator">
      <Setter Property="Background" Value="DarkBlue"/>
      <Setter Property="Width" Value="2"/>
    </Style>

    <Style TargetType="Button">
      <Setter Property="Foreground" Value="Blue"/>
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="HorizontalAlignment" Value="Center"/>
      <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>

    <Style TargetType="CheckBox">
      <Setter Property="Foreground" Value="DarkSlateBlue"/>
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="HorizontalAlignment" Value="Center"/>
      <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>

    <Style TargetType="Menu">
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="FontStyle" Value="Italic"/>
      <Setter Property="FontWeight" Value="Bold"/>
      <Setter Property="Background" Value="LightSteelBlue"/>
    </Style>

    <Style TargetType="RadioButton">
      <Setter Property="Background" Value="LightSteelBlue"/>
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="HorizontalAlignment" Value="Center"/>
      <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>

    <Style TargetType="TextBox">
      <Setter Property="Background" Value="DarkBlue"/>
      <Setter Property="Foreground" Value="White"/>
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="FontStyle" Value="Italic"/>
      <Setter Property="HorizontalAlignment" Value="Center"/>
      <Setter Property="VerticalAlignment" Value="Center"/>
      <Setter Property="Width" Value="75"/>
    </Style>

    <Style TargetType="ComboBox">
      <Setter Property="Background" Value="LightSteelBlue"/>
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="MinWidth" Value="60"/>
      <Setter Property="HorizontalAlignment" Value="Center"/>
      <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>
  </Window.Resources>

  <StackPanel>
    <ToolBarTray Margin="10,10,3,3" 
                 Grid.Column="0" Grid.Row="2"
                 Background="LightBlue">
      <ToolBar >
        <Button Content="Button 1"/>
        <Button Content="Button 2"/>
        <Separator/>
        <CheckBox Content="CheckBox 1"/>
        <CheckBox Content="CheckBox 2"/>
        <Separator/>
        <RadioButton>One</RadioButton>
        <RadioButton>Two</RadioButton>
        <Separator/>
        <ComboBox>
          <ComboBoxItem IsSelected="True">Item 1</ComboBoxItem>
          <ComboBoxItem>Item 2</ComboBoxItem>
          <ComboBoxItem>Item 3</ComboBoxItem>
          <ComboBoxItem>Item 4</ComboBoxItem>
        </ComboBox>
        <TextBox/>
        <Separator/>
        <Menu>
          <MenuItem Header="Menu">
            <MenuItem Header="File">
              <MenuItem Header="Copy"/>
              <MenuItem Header="Paste"/>
            </MenuItem>
          </MenuItem>
        </Menu>
      </ToolBar>
    </ToolBarTray>


  </StackPanel>
</Window>

   
    
    
    
     


Compare the appearances of these controls with the ones in the ToolBar.


   
    



<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Background="White">
  <Window.Resources>
    <Style x:Key="{x:Static ToolBar.SeparatorStyleKey}" TargetType="Separator">
      <Setter Property="Background" Value="DarkBlue"/>
      <Setter Property="Width" Value="2"/>
    </Style>

    <Style x:Key="{x:Static ToolBar.ButtonStyleKey}" TargetType="Button">
      <Setter Property="Foreground" Value="Blue"/>
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="HorizontalAlignment" Value="Center"/>
      <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>

    <Style x:Key="{x:Static ToolBar.CheckBoxStyleKey}" TargetType="CheckBox">
      <Setter Property="Foreground" Value="DarkSlateBlue"/>
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="HorizontalAlignment" Value="Center"/>
      <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>
    
    <Style x:Key="{x:Static ToolBar.MenuStyleKey}" TargetType="Menu">
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="FontStyle" Value="Italic"/>
      <Setter Property="FontWeight" Value="Bold"/>
      <Setter Property="Background" Value="LightSteelBlue"/>
    </Style>
    
    <Style x:Key="{x:Static ToolBar.RadioButtonStyleKey}" TargetType="RadioButton">
      <Setter Property="Background" Value="LightSteelBlue"/>
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="HorizontalAlignment" Value="Center"/>
      <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>
   
    <Style x:Key="{x:Static ToolBar.TextBoxStyleKey}" TargetType="TextBox">
      <Setter Property="Background" Value="DarkBlue"/>
      <Setter Property="Foreground" Value="White"/>
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="FontStyle" Value="Italic"/>
      <Setter Property="HorizontalAlignment" Value="Center"/>
      <Setter Property="VerticalAlignment" Value="Center"/>
      <Setter Property="Width" Value="75"/>
    </Style>
    
    <Style x:Key="{x:Static ToolBar.ComboBoxStyleKey}" TargetType="ComboBox">
      <Setter Property="Background" Value="LightSteelBlue"/>
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="MinWidth" Value="60"/>
      <Setter Property="HorizontalAlignment" Value="Center"/>
      <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>
    
    <Style TargetType="Separator">
      <Setter Property="Background" Value="DarkBlue"/>
      <Setter Property="Width" Value="2"/>
    </Style>

    <Style TargetType="Button">
      <Setter Property="Foreground" Value="Blue"/>
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="HorizontalAlignment" Value="Center"/>
      <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>

    <Style TargetType="CheckBox">
      <Setter Property="Foreground" Value="DarkSlateBlue"/>
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="HorizontalAlignment" Value="Center"/>
      <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>

    <Style TargetType="Menu">
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="FontStyle" Value="Italic"/>
      <Setter Property="FontWeight" Value="Bold"/>
      <Setter Property="Background" Value="LightSteelBlue"/>
    </Style>

    <Style TargetType="RadioButton">
      <Setter Property="Background" Value="LightSteelBlue"/>
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="HorizontalAlignment" Value="Center"/>
      <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>

    <Style TargetType="TextBox">
      <Setter Property="Background" Value="DarkBlue"/>
      <Setter Property="Foreground" Value="White"/>
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="FontStyle" Value="Italic"/>
      <Setter Property="HorizontalAlignment" Value="Center"/>
      <Setter Property="VerticalAlignment" Value="Center"/>
      <Setter Property="Width" Value="75"/>
    </Style>

    <Style TargetType="ComboBox">
      <Setter Property="Background" Value="LightSteelBlue"/>
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="MinWidth" Value="60"/>
      <Setter Property="HorizontalAlignment" Value="Center"/>
      <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>
  </Window.Resources>

  <StackPanel>

    <WrapPanel Background="LightBlue"
               Margin="10,10,3,3" 
               Grid.Column="0" Grid.Row="4">
      <Button Margin="2" Content="Button 1"/>
      <Button Margin="2" Content="Button 2"/>
      <CheckBox Margin="2" Content="CheckBox 1"/>
      <CheckBox Margin="2" Content="CheckBox 2"/>
      <RadioButton Margin="2">One</RadioButton>
      <RadioButton Margin="2">Two</RadioButton>
      <ComboBox Margin="2">
        <ComboBoxItem IsSelected="True">Item 1</ComboBoxItem>
        <ComboBoxItem>Item 2</ComboBoxItem>
        <ComboBoxItem>Item 3</ComboBoxItem>
        <ComboBoxItem>Item 4</ComboBoxItem>
      </ComboBox>
      <TextBox Margin="2"/>
      <Menu Margin="2">
        <MenuItem Header="Menu">
          <MenuItem Header="File">
            <MenuItem Header="Copy"/>
            <MenuItem Header="Paste"/>
          </MenuItem>
        </MenuItem>
      </Menu>
    </WrapPanel>


  </StackPanel>
</Window>

   
    
    
    
     


A hierarchy of timelines

   
     
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      HorizontalAlignment="Stretch" VerticalAlignment="Stretch">


<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
  <StackPanel.Triggers>
    <EventTrigger RoutedEvent="StackPanel.Loaded">
      <BeginStoryboard>
        <Storyboard>
          <ParallelTimeline RepeatBehavior="Forever">

            <DoubleAnimation BeginTime="0:0:0" Duration="0:0:0.2"
                Storyboard.TargetName="button1"
                Storyboard.TargetProperty="(Button.Height)"
                By="30" AutoReverse="True" />

            <DoubleAnimation BeginTime="0:0:1" Duration="0:0:0.2"
                Storyboard.TargetName="button2"
                Storyboard.TargetProperty="(Button.Height)"
                By="30" AutoReverse="True" />

            <ParallelTimeline BeginTime="0:0:2">

            <DoubleAnimation BeginTime="0:0:0" Duration="0:0:0.2"
                  Storyboard.TargetName="button3"
                  Storyboard.TargetProperty="(Button.Height)"
                  By="30" AutoReverse="True" />


            </ParallelTimeline>

          </ParallelTimeline>
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </StackPanel.Triggers>

  <Button Name="button1" Height="25">One</Button>
  <Button Name="button2" Height="25">Two</Button>
  <Button Name="button3" Height="25">Three</Button>
</StackPanel>

</Page>

   
    
    
    
    
     


Implicit duration of parent timeline


   
     
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
  <StackPanel.Triggers>
    <EventTrigger RoutedEvent="StackPanel.Loaded">
      <BeginStoryboard>
        <Storyboard RepeatBehavior="Forever">

            
            <ParallelTimeline>
            
              <DoubleAnimation BeginTime="0:0:0" Duration="0:0:0.2"
                    Storyboard.TargetName="button1"
                    Storyboard.TargetProperty="(Button.Height)"
                    By="30" />
            
              <DoubleAnimation BeginTime="0:0:1" Duration="0:0:0.2"
                    Storyboard.TargetName="button2"
                    Storyboard.TargetProperty="(Button.Height)"
                    By="30" />
            
            </ParallelTimeline>


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

  <Button Name="button1" Height="25">One</Button>
  <Button Name="button2" Height="25">Two</Button>
</StackPanel>

</Page>

   
    
    
    
    
     


Set interval and event handler for DispatcherTimer


   
  


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="KeyboardInput" Height="300" Width="300">
    <Grid>
        
    </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.Windows.Threading;
using System.Diagnostics;


namespace WpfApplication1
{
    public partial class Window1 : System.Windows.Window
    {
        DispatcherTimer dt = new DispatcherTimer();
        public Window1()
        {
            InitializeComponent();
            dt.Interval = TimeSpan.FromSeconds(0.5);
            dt.Tick += new EventHandler(dt_Tick);
            dt.Start();
        }

        void dt_Tick(object sender, EventArgs e)
        {
         
            if ((Keyboard.Modifiers &amp; ModifierKeys.Control) != 0)
            {
                Console.WriteLine("ModifierKeys.Control");
            }
            bool homeKeyPressed = Keyboard.IsKeyDown(Key.Home);
            Debug.WriteLine("Home pressed: " + homeKeyPressed);
        }

    }
}

   
    
     


Keep the UI from becoming non-responsive in single threaded application which performs a long operation.


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Prime Numbers" Width="260" Height="75">
  <StackPanel Orientation="Horizontal" VerticalAlignment="Center" >
    <Button Content="Start" Click="StartOrStop" Name="startStopButton" Margin="5,0,5,0"/>
    <TextBlock Margin="10,5,0,0">Number:</TextBlock>
    <TextBlock Name="numberTextBlock" Margin="4,5,0,0">3</TextBlock>
  </StackPanel>
</Window>
//File:Window.xaml.cs

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

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public delegate void NextPrimeDelegate();
        private long num = 1;   

        private bool continueCalculating = false;

        public Window1() : base()
        {
            InitializeComponent();
        }
        public void StartOrStop(object sender, EventArgs e)
        {
            if (continueCalculating)
            {
                continueCalculating = false;
                startStopButton.Content = "Resume";
            }
            else
            {
                continueCalculating = true;
                startStopButton.Content = "Stop";
                startStopButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal,new NextPrimeDelegate(CheckNextNumber));
            }
        }
        public void CheckNextNumber()
        {
            numberTextBlock.Text = num.ToString();
            num += 2;
            if (continueCalculating)
            {
                startStopButton.Dispatcher.BeginInvoke(
                    System.Windows.Threading.DispatcherPriority.SystemIdle, 
                    new NextPrimeDelegate(this.CheckNextNumber));
            }
        }
    }
}