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.
<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
Comments
Post a Comment