Monday 2 April 2012

EventTrigger In Wpf:-

If you want to perform animations on mouse events we can use EventTriggers.Using EventTriggers we can start,pause and stop the storyboards.The following example  performs simple animations on mouse enter and mouse leave events.

<Window x:Class="WpfEventTriggers.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Rectangle Name="rMain" Width="100" Height="100">
            <Rectangle.Fill>
                <SolidColorBrush x:Name="MySolidColorBrush" Color="Green" />
            </Rectangle.Fill>
            <Rectangle.Triggers>
                <!--Begins the storyboard on mouseenter-->
                <EventTrigger RoutedEvent="MouseEnter">
                    <BeginStoryboard  Name="sbMain">
                        <!--StoryBoard-->
                        <Storyboard>
                            <DoubleAnimation
                                            Storyboard.TargetName="rMain"
                                            Storyboard.TargetProperty="Width"
                                            From="100" To="200" Duration="0:0:1" >
                            </DoubleAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>

                <!--Stops the storyboard on mouseleave-->
                <EventTrigger RoutedEvent="MouseLeave">
                    <StopStoryboard>
                        <StopStoryboard.BeginStoryboardName>
                            <!--Specify storyboard name-->
                            sbMain
                        </StopStoryboard.BeginStoryboardName>
                    </StopStoryboard>
                </EventTrigger>
            </Rectangle.Triggers>
        </Rectangle>
    </Grid>
</Window>

Let me know, if you have any feedback. Mail me for source code. Enjoy reading my articles…
sekhartechblog@gmail.com

No comments:

Post a Comment