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)
        {
        }

    }
}

   
    
     


Update the UI Asynchronously on a Timer


   
  

<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="300">
    <StackPanel>
        <Button x:Name="button" Click="Button_Click">Start Timer</Button>
        <TextBlock x:Name="txtStatus">
        </TextBlock>
    </StackPanel>
</Window>

//File:Window.xaml.cs

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

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        private DispatcherTimer timer;
         
        public Window1()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if(timer == null || !timer.IsEnabled)
            {
                timer = new DispatcherTimer();

                timer.Interval = TimeSpan.FromMilliseconds(1000);
                timer.Tick += new EventHandler(timer_Tick);

                timer.Start();
                button.Content = "Stop Timer";
            }
            else
            {
                timer.Stop();
                button.Content = "Start Timer";
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            txtStatus.Text = DateTime.Now.Second.ToString();
        }
    }
}

   
    
     


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

    }
}

   
    
     


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>

   
    
    
    
    
     


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>

   
    
    
    
    
     


Thumb DragStarted and DragCompleted event handler


   
  
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Thumb_wcp.Pane1">
  <StackPanel>
    <Thumb Name="myThumb" Background="Blue" 
          Width="20" Height="20" 
          DragStarted="onDragStarted" DragCompleted="onDragCompleted"/>
  </StackPanel>
</Canvas>

//File:Window.xaml.cs

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

namespace Thumb_wcp
{
    public partial class Pane1 : Canvas
    {

        void onDragStarted(object sender, DragStartedEventArgs e)
        {
            myThumb.Background = Brushes.Orange;
        }
        void onDragCompleted(object sender, DragCompletedEventArgs e)
        {
            myThumb.Background = Brushes.Blue;
        }
    }
}

   
    
     


Use a Thumb to resize a Canvas control by responding to the DragDelta event.


   
  

<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Thumb_wcp.Pane1">
  <Canvas Width="100" Height="100" Name="myCanvasStretch">
    <TextBox Name="changes" 
         Width="{Binding ElementName=myCanvasStretch,Path=Width}"  
         Height="{Binding ElementName=myCanvasStretch,Path=Height}" 
         Text="Size: 100, 100"/>
    <Thumb Name="myThumb" Canvas.Left="80" Canvas.Top="80" Background="Blue" 
          Width="20" Height="20" DragDelta="onDragDelta"/>
  </Canvas>
</Canvas>
//File:Window.xaml.cs


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

namespace Thumb_wcp
{
    public partial class Pane1 : Canvas
    {
        void onDragDelta(object sender, DragDeltaEventArgs e)
        {
            double yadjust = myCanvasStretch.Height + e.VerticalChange;
            double xadjust = myCanvasStretch.Width + e.HorizontalChange;
            if ((xadjust >= 0) &amp;&amp; (yadjust >= 0))
            {
                myCanvasStretch.Width = xadjust;
                myCanvasStretch.Height = yadjust;
                Canvas.SetLeft(myThumb, Canvas.GetLeft(myThumb) + e.HorizontalChange);
                Canvas.SetTop(myThumb, Canvas.GetTop(myThumb) + e.VerticalChange);
                Console.WriteLine(myCanvasStretch.Width);
                Console.WriteLine(myCanvasStretch.Height);
            }
        }

    }
}