SIGN UP MEMBER LOGIN:    
ARTICLE

Triggers in Silverlight

Posted by Mahadesh Mahalingappa Articles | Silverlight with C# August 10, 2011
In this article we are going to look at how we can create Triggers in Silverlight and what do they offer for us.
Reader Level:
Download Files:
 

In this article we are going to look at how we can create Triggers in Silverlight and what do they offer for us.

First Step:

Install Blend 4 SDK from the Microsoft Site.

http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=3062

The Dll we require to implement the triggers is Microsoft.Expression.Interactions.dll.

Please find the screenshot below :

Creating Triggers in Silverlight

Created a new Silverlight Project and named it as SilverlightTriggers .

Add a reference to the Dll Microsoft.Expression.Interactions.dll .

Creating Triggers in Silverlight

Lets create a simple trigger to animate the border of the Button .

No code Behind is required . All the work has to be done on the Xaml .

Lets go one step at a time .

Add a Button to the Grid .

<Grid x:Name="LayoutRoot" Background="White">
        <Button Content="Button" Height="87" HorizontalAlignment="Left" Margin="219,113,0,0" Name="Btn" VerticalAlignment="Top" Width="148">
        </Button>

</Grid>

Add the namespaces to the Button Tag .

<Grid x:Name="LayoutRoot" Background="White">
        <Button xmlns:interactivity="http://schemas.microsoft.com/expression/2010/interactivity"
                xmlns:interactions="http://schemas.microsoft.com/expression/2010/interactions"           
            Content="Button" Height="87" HorizontalAlignment="Left" Margin="219,113,0,0" Name="Btn" VerticalAlignment="Top" Width
="148">
        </Button>

    </Grid>

Add the StoryBoard in the Resource Tag of the Button .

<Grid x:Name="LayoutRoot" Background="White">
        <Button xmlns:interactivity="http://schemas.microsoft.com/expression/2010/interactivity"
                xmlns:interactions="http://schemas.microsoft.com/expression/2010/interactions"           
            Content="Button" Height="87" HorizontalAlignment="Left" Margin="219,113,0,0" Name="Btn" VerticalAlignment="Top" Width
="148">
            <Button.Resources>
                <Storyboard x:Key="Story" Duration="200"
                        Storyboard.TargetName="Btn"
                        Storyboard.TargetProperty
="BorderBrush">
                    <ObjectAnimationUsingKeyFrames>
                        <DiscreteObjectKeyFrame KeyTime="0:0:0.1" Value="Yellow" />
                        <DiscreteObjectKeyFrame KeyTime="0:0:0.2" Value="{x:Null}" />
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </Button.Resources>
        </Button>

    </Grid>

Finally Add the Trigger . I have added a Simple Trigger which performs the play operation on the storyboard .

<Grid x:Name="LayoutRoot" Background="White">
        <Button xmlns:interactivity="http://schemas.microsoft.com/expression/2010/interactivity"
                xmlns:interactions="http://schemas.microsoft.com/expression/2010/interactions"           
            Content="Button" Height="87" HorizontalAlignment="Left" Margin="219,113,0,0" Name="Btn" VerticalAlignment="Top" Width
="148">
            <Button.Resources>
                <Storyboard x:Key="Story" Duration="200"
                        Storyboard.TargetName="Btn"
                        Storyboard.TargetProperty
="BorderBrush">
                    <ObjectAnimationUsingKeyFrames>
                        <DiscreteObjectKeyFrame KeyTime="0:0:0.1" Value="Yellow" />
                        <DiscreteObjectKeyFrame KeyTime="0:0:0.2" Value="{x:Null}" />
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </Button.Resources>
          
     
<interactivity:Interaction.Triggers>
                <interactivity:EventTrigger EventName="Click">
                    <interactions:ControlStoryboardAction Storyboard="{StaticResource Story}" ControlStoryboardOption="Play" />
                </interactivity:EventTrigger>
            </interactivity:Interaction.Triggers>
        </Button>

    </Grid>

Output

The Button is displayed as shown below :

Creating Triggers in Silverlight

Click on it and you can see it animate . Well you can animate it differently by changing the storyboard . This is just a illustration . Happy Coding .

Login to add your contents and source code to this article
share this article :
post comment
 

excellent stuff regarding silverlight http://soft-engineering.blogspot.com

Posted by chetan vaghela Nov 23, 2011

Nice Article..........

Posted by Angelina Erin Aug 11, 2011
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Become a Sponsor