EventTrigger Ellipse.MouseLeftButtonDown

image_pdfimage_print


   
     

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      HorizontalAlignment="Center" VerticalAlignment="Center">
        
        <Ellipse Name="myEllipse" Fill="Red" Height="100" Width="10">
          <Ellipse.Triggers>
            <EventTrigger RoutedEvent="Ellipse.MouseEnter">
              <BeginStoryboard Name="changeWidth">
                <Storyboard>
                  <DoubleAnimation Storyboard.TargetProperty="(Ellipse.Width)"
                                   To="300" Duration="0:0:5" />
                </Storyboard>
              </BeginStoryboard>
            </EventTrigger>
        
            <EventTrigger RoutedEvent="Ellipse.MouseLeftButtonDown">
              <PauseStoryboard BeginStoryboardName="changeWidth" />
            </EventTrigger>
          </Ellipse.Triggers>
        </Ellipse>
</Page>

   
    
    
    
    
     


EventTrigger Ellipse.MouseLeftButtonUp

image_pdfimage_print


   
     

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      HorizontalAlignment="Center" VerticalAlignment="Center">
        
        <Ellipse Name="myEllipse" Fill="Red" Height="100" Width="10">
          <Ellipse.Triggers>
            <EventTrigger RoutedEvent="Ellipse.MouseEnter">
              <BeginStoryboard Name="changeWidth">
                <Storyboard>
                  <DoubleAnimation Storyboard.TargetProperty="(Ellipse.Width)"
                                   To="300" Duration="0:0:5" />
                </Storyboard>
              </BeginStoryboard>
            </EventTrigger>
        
            <EventTrigger RoutedEvent="Ellipse.MouseLeftButtonUp">
              <ResumeStoryboard BeginStoryboardName="changeWidth" />
            </EventTrigger>
          </Ellipse.Triggers>
        </Ellipse>
</Page>

   
    
    
    
    
     


Playing a sound with a trigger

image_pdfimage_print


   
     

        

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

        <Rectangle Fill="Green">
          <Rectangle.Triggers>
            <EventTrigger RoutedEvent="Rectangle.MouseEnter">
              <SoundPlayerAction Source="c:windowsmediading.wav" />
            </EventTrigger>
          </Rectangle.Triggers>
        </Rectangle>
</Page>

   
    
    
    
    
     


Trigger Property=FrameworkElement.IsFocused

image_pdfimage_print


   
     

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Page.Resources>
        <Style x:Key="widthOnFocus">
          <Style.Triggers>
            <Trigger Property="FrameworkElement.IsFocused" Value="True">
              <Trigger.EnterActions>
                <BeginStoryboard Name="changeWidth">
                  <Storyboard>
                    <DoubleAnimation Storyboard.TargetProperty="(FrameworkElement.Width)" To="300" Duration="0:0:5" />
                  </Storyboard>
                </BeginStoryboard>
              </Trigger.EnterActions>
              <Trigger.ExitActions>
                <RemoveStoryboard BeginStoryboardName="changeWidth" />
              </Trigger.ExitActions>
            </Trigger>
          </Style.Triggers>
        </Style>
    </Page.Resources>
    
    <StackPanel>
      <TextBox Style="{StaticResource widthOnFocus}" Width="200" />
    </StackPanel>


</Page>

   
    
    
    
    
     


Cross Thread Exception Raising

image_pdfimage_print
   
  
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="WpfApplication1.SecondaryUiThreadWindow"
  Height="200" Width="400" Closed="SecondaryUiThreadWindow_Closed">
  <Button Click="r">Raise an Exception on the Secondary UI Thread</Button>

</Window>
//File:Window.xaml.cs
using System; 
using System.Threading;
using System.Windows; 
using System.Windows.Threading;

namespace WpfApplication1
{
    public partial class SecondaryUiThreadWindow : Window
    {
        public SecondaryUiThreadWindow()
        {
            InitializeComponent();

            this.Title = Thread.CurrentThread.ManagedThreadId+"";
        }
        void r(object sender, RoutedEventArgs e)
        {
            throw new Exception(Dispatcher.CurrentDispatcher.Thread.ManagedThreadId+"");
        }
        void SecondaryUiThreadWindow_Closed(object sender, EventArgs e)
        {
            Dispatcher.CurrentDispatcher.InvokeShutdown();
        }
    }
}

   
    
     


Cross Thread Exception Raising WorkerThread

image_pdfimage_print
   
  

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="WpfApplication1.App"
  StartupUri="MainWindow.xaml" 
  DispatcherUnhandledException="App_DispatcherUnhandledException" />

//File:Window.xaml.cs
using System; 
using System.Text;
using System.Windows;
using System.Windows.Threading; 

namespace WpfApplication1
{
    public partial class App : Application
    {
        void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            Console.WriteLine(e.Exception.InnerException.Message);
            Console.WriteLine(e.Exception.Message);
            Console.WriteLine(e.Dispatcher.Thread.ManagedThreadId);
         

            e.Handled = true;
        }
    }
}

   
    
     


Check Exception type for DispatcherUnhandledException

image_pdfimage_print
   
  
<Application 
    x:Class="DispatcherUnhandledExceptionSample.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="MainWindow.xaml" 
    DispatcherUnhandledException="App_DispatcherUnhandledException" />

//File:Window.xaml.cs
using System;
using System.Diagnostics;
using System.Windows;
using System.Data;
using System.Xml;
using System.Configuration;
using System.Windows.Threading;

namespace DispatcherUnhandledExceptionSample
{
    public partial class App : Application
    {
        void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            bool shutdown = false;

            if (e.Exception is DivideByZeroException)
            {
                shutdown = false;
            }
            else if (e.Exception is ArgumentNullException)
            {
                shutdown = true;
            }

            if (shutdown)
            {
                MessageBoxResult result = MessageBox.Show("Application must exit:

" + e.Exception.Message + "

Save before exit?", "app", MessageBoxButton.YesNo, MessageBoxImage.Error);
                if (result == MessageBoxResult.Yes)
                {
                }
                EventLog.WriteEntry("app", "Unrecoverable Exception: " + e.Exception.Message, EventLogEntryType.Error);
                this.Shutdown(-1);
            }
            e.Handled = true;
        }
    }
}