Kailash's Blogs

WPF Event Trigger

Kailash Chandra Behera | Saturday, July 23, 2016
Kailash's Blogs


This article describes what is WPF Event Trigger and demonstrates use of Event Trigger. In this demonstration, we have taken a Button control, changes the Backgroung and Foreground color of Button using Event Trigger.

Getting Started

This trigger is same like PropertyTrigger and DataTrigger, it gets executed when event of control(Target element) is fired like MouseEnter and MouseLeave. Mostly the Even Trigger is used to accomplish some animation on control such DoubleAnumatio, ColorAnimation and PointAnimation etc.

The below example demonstrates how to use Event Trigger in WPF. In this example the Backgroung and Foreground color of Button is changing by using Event Trigger and ColorAnimation. When the mouse enters into the Button the Foreground color of the button gets changed into Yellow and the background color gets changed into Green and when Mouse leaves background color changes into blue and foreground color into red.

     <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="10,35,0,0" VerticalAlignment="Top" Width="200" Height="200">  
             <EventTrigger RoutedEvent="Button.MouseEnter">  
                   <ColorAnimation Storyboard.TargetProperty="(Button.Foreground).(SolidColorBrush.Color)" To="Yellow" Duration="0:0:1"/>  
                   <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="Green" Duration="0:0:2"/>  
             <EventTrigger RoutedEvent="Button.MouseLeave">  
                   <ColorAnimation Storyboard.TargetProperty="(Button.Foreground).(SolidColorBrush.Color)" To="Red" Duration="0:0:1"/>  
                   <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="Navy" Duration="0:0:1"/>  

Related Articles

  1. WPF Data Trigger
  2. WPF Property Trigger
  3. UpdateSourceTrigger
  4. WPF Triggers
  5. WPF Converters


In the above of this article we have seen how to use WPF Event Trigger, hope this article may helpful to you.