Triggers in Silverlight 3

Posted by Corey on August 13, 2009 at 5:22 pm.

Did you know there is a built-in Trigger in Silverlight 3?  It’s the the Loaded event of objects.  There’s been a fair bit of attention given to Behaviors, and rightfully so, but sometimes you simply want a trigger.

It would be great to have more Triggers in SL, but for now, let’s take a look at the Loaded trigger.  It’s particularly interesting because you can handle the visuals of an element becoming visible.  Adding a build on (like fade in) transition to your elements in your application is something small, but adds polish, and should be encouraged.

The below Xaml shows the fading in of the LayoutRoot using the Loaded Trigger.


<Grid x:Name="LayoutRoot" Opacity="1">
  <Grid.Triggers>
    <EventTrigger RoutedEvent="Grid.Loaded">
      <BeginStoryboard>
        <Storyboard x:Name="LayoutRootLoaded">
          <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
              Storyboard.TargetName="LayoutRoot"
              Storyboard.TargetProperty="(UIElement.Opacity)">
            <EasingDoubleKeyFrame KeyTime="00:00:00" Value="0" />
            <EasingDoubleKeyFrame KeyTime="00:00:05" Value="1">
              <EasingDoubleKeyFrame.EasingFunction>
                <ExponentialEase EasingMode="EaseOut" />
              </EasingDoubleKeyFrame.EasingFunction>
            </EasingDoubleKeyFrame>
          </DoubleAnimationUsingKeyFrames>
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </Grid.Triggers>

Unfortunately I haven’t been able to find a way to edit a Storyboard defined in a Trigger.  For right now you’ll have to create the Storyboard in the Resources and copy into the Trigger.

One Response to “Triggers in Silverlight 3”

Leave a Reply