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 over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies. Demonstrated expertise in delivering all phases of project development—from initiation to closure—while aligning with business objectives to drive process improvements, competitive advantage, and measurable bottom-line gains. Proven ability to work independently and manage multiple projects successfully. Committed to the efficient and effective development of projects in fast-paced, deadline-driven environments. Skills: Proficient in designing and developing applications using various Microsoft technologies. Total IT Experience: 13+ years

Previous Post Next Post

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