WinRTTriggers allows you to declaratively configure interactions between your UI, view modal and end user. This is very similar in nature to the triggers and behaviors functionality that Expression provided for Silverlight applications – these, unfortunately, are not currently available for WinRT applications.

For example, the following XAML invokes a command when the Tapped event is fired on a Canvas control:

<Canvas x:Name="picture" Height="50" Width="50">
    <Triggers:Interactions.Triggers>
        <Triggers:EventTrigger EventName="Tapped">
            <Triggers:InvokeCommandAction Command="{Binding ToggleStateCommand}" />
        </Triggers:EventTrigger>
    </Triggers:Interactions.Triggers>

    <Ellipse x:Name="ellipse" Width="50" Height="50" 
        Stroke="#FF575B06" Fill="#FFFBFFAD" StrokeThickness="2" />
</Canvas>

You could easily swap out the invoke command action for something more interesting, like starting a storyboard:

<Canvas x:Name="picture" Height="50" Width="50">
    <Triggers:Interactions.Triggers>
        <Triggers:EventTrigger EventName="Tapped">
            <Triggers:ControlStoryboardAction Action="Start" 
                             Storyboard="{StaticResource FlashImage}" />
        </Triggers:EventTrigger>
    </Triggers:Interactions.Triggers>

    <Ellipse x:Name="ellipse" Width="50" Height="50" 
        Stroke="#FF575B06" Fill="#FFFBFFAD" StrokeThickness="2" />
</Canvas>

WinRTTriggers is available to download from nuget.

Supported Triggers

PropertyChangedTrigger

Fired when a property changes to any value

PropertySetTrigger

Similar to PropertyChangedTrigger, except it will only get fire when a property changes to a specified value.

EventTrigger

Fires when an event associated to the control is fired, e.g. the Tapped event on a control.

v1.4: EventTrigger also supports hooking into events on another object, e.g. your view model - just set the SourceObject property to the target object. Leaving this unset or null will result in the event being attached to the associated XAML object.

StoryboardCompletedTrigger

Fires when a storyboard completes.

Supported Actions

GotoStateAction

Instructs the VisualStateManager to change to a named state

InvokeCommandAction

Invokes some ICommand implementation, probably located on your view model.

ControlStoryboardAction

Starts/Stops/Pauses a storyboard.

SetPropertyAction

Allows you to set a property on an object (probably your view model) when a trigger is fired.

Demo Application

The sample application that comes with the source code demonstrates the use of several combinations of the supported triggers and actions – check it out and have a play with it.

WinRTTriggersScreenshot

Links

I’ll be posting information about this project on my blog – you could also hassle me on Twitter.

Last edited Jun 7, 2013 at 8:40 AM by MikeGoatly, version 8