Click here to Skip to main content
15,898,035 members
Articles / Desktop Programming / XAML

Silverlight Experimental Hacks (SLEX) - EventTrigger, PropertyTrigger, ReactiveTrigger, InvokeMethodAction, StoryBoardAction, etc. for Silverlight

Rate me:
Please Sign up or sign in to vote.
4.81/5 (8 votes)
14 Jan 2010CPOL6 min read 41K   241   24  
A set of Silverlight Experimental Hacks (1) A custom implementation of EventTrigger and PropertyTrigger (2) Invoking methods in your view model in MVVM (3) Conditionally invoking triggers and behaviors (4) ReactiveTrigger for exporting your custom events
<UserControl x:Class="Slex.Lib.Demo.Views.Demos.StoryBoardAction"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"      
    xmlns:slex="clr-namespace:Slex.Lib.Interactions;assembly=Slex.Lib"                   
    d:DesignHeight="300" d:DesignWidth="400">

    <Canvas>

        <i:Interaction.Triggers>
            <slex:EventTrigger ElementName="btnBegin" EventName="Click">
                <slex:StoryBoardAction StoryBoardName="myStoryboard" Action="Begin"/>
            </slex:EventTrigger>
            <slex:EventTrigger ElementName="btnStop" EventName="Click">
                <slex:StoryBoardAction StoryBoardName="myStoryboard" Action="Stop"/>
            </slex:EventTrigger>
            <slex:EventTrigger ElementName="btnPause" EventName="Click">
                <slex:StoryBoardAction StoryBoardName="myStoryboard" Action="Pause"/>
            </slex:EventTrigger>
            <slex:EventTrigger ElementName="btnResume" EventName="Click">
                <slex:StoryBoardAction StoryBoardName="myStoryboard" Action="Resume"/>
            </slex:EventTrigger>
        </i:Interaction.Triggers>
        
        <Canvas.Resources>
            <Storyboard x:Name="myStoryboard">
                <!-- Animate the center point of the ellipse. -->
                <PointAnimation Storyboard.TargetProperty="Center"
          Storyboard.TargetName="MyAnimatedEllipseGeometry"
          Duration="0:0:5"
          From="20,200"
          To="400,100"
          RepeatBehavior="Forever" />
            </Storyboard>
        </Canvas.Resources>

        <Path Fill="Blue">
            <Path.Data>

                <!-- Describe an ellipse. -->

                <EllipseGeometry x:Name="MyAnimatedEllipseGeometry"
          Center="20,20" RadiusX="15" RadiusY="15" />
            </Path.Data>
        </Path>

        <StackPanel Orientation="Horizontal" Canvas.Left="10" Canvas.Top="265">

            <!-- Button that begins animation. -->

            <Button Name="btnBegin"
        Width="65" Height="30" Margin="2" Content="Begin" />


            <!-- Button that pauses animation. -->

            <Button Name="btnPause"
        Width="65" Height="30" Margin="2" Content="Pause" />


            <!-- Button that resumes animation. -->

            <Button  Name="btnResume"
        Width="65" Height="30" Margin="2" Content="Resume" />


            <!-- Button that stops animation. Stopping the animation returns the
        ellipse to its original location. -->

            <Button  Name="btnStop"
        Width="65" Height="30" Margin="2" Content="Stop" />
        </StackPanel>

    </Canvas>

</UserControl>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
Architect, Developer, Speaker | Wannabe GUT inventor & Data Scientist | Microsoft MVP in C#

Comments and Discussions