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
Summary
In the above of this article we have seen how to use WPF Event Trigger, hope this article may helpful to you.
Thanks