WPF Event Trigger

Introduction

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.

  <Grid>  
     <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="10,35,0,0" VerticalAlignment="Top" Width="200" Height="200">  
       <Button.Style>  
         <Style>  
           <Style.Triggers>  
             <EventTrigger RoutedEvent="Button.MouseEnter">  
               <BeginStoryboard>  
                 <Storyboard>  
                   <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"/>  
                 </Storyboard>  
               </BeginStoryboard>  
             </EventTrigger>  
             <EventTrigger RoutedEvent="Button.MouseLeave">  
               <BeginStoryboard>  
                 <Storyboard>  
                   <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"/>  
                 </Storyboard>  
               </BeginStoryboard>  
             </EventTrigger>  
           </Style.Triggers>  
         </Style>  
       </Button.Style>  
     </Button>  
   </Grid>  

Related Articles

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

Summary

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

Thanks

Kailash Chandra Behera

An IT Professional with 12 years experience in development life cycle in windows, service and Web based application using Microsoft.Net technologies. Proven record of developing all phases of projects in Microsoft.Net technology from initiation to closure aligning with the company's Business objectives to drive process improvements, competitive advantage and bottom-line gains. -> Good exposure of independently working and developing multiple projects ->Committed to efficient and effective development of projects in a fast-paced and deadline driver environment. Skill :- Develop and design projects in various technologies of Microsoft Technology. Total IT Experience- 13+

Previous Post Next Post

نموذج الاتصال