共用方式為


EventTrigger

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Represents a trigger that applies a set of actions (animation storyboards) in response to an event.

<EventTrigger>
  oneOrMoreBeginStoryboards
</EventTrigger>

XAML Values

Value

Description

oneOrMoreBeginStoryboards

One or more BeginStoryboard object elements. Object elements defined here become members of the Actions collection when scripting accesses the Actions property at run time.

Managed Equivalent

EventTrigger

Remarks

You do not typically specify Actions as an explicit property element in XAML, because it is the default collection property of an EventTrigger. You will set the Actions value in XAML by placing one or more BeginStoryboard elements as the child elements of an EventTrigger object element.

When EventTrigger itself is specified in XAML, it is always a child element of the <object.Triggers> property element. The Triggers property requires the property element syntax to set it in XAML.

In Silverlight, the only event that you can use for an EventTrigger is the Loaded event. For other events, you should declare a storyboard in the Resources property, provide a Name value for the storyboard, and write an event handler that calls the Begin method on the named storyboard.

Example

The following example uses a Storyboard object and an EventTrigger to make a rectangle that fades in and out of view after it is loaded.

<Canvas
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml">
  <Rectangle
    x:Name="MyAnimatedRectangle"
    Width="100"
    Height="100"
    Fill="Blue">
    <Rectangle.Triggers>

      <!-- Animates the rectangle's opacity. -->
      <EventTrigger RoutedEvent="Rectangle.Loaded">
        <BeginStoryboard>
          <Storyboard>
            <DoubleAnimation
              Storyboard.TargetName="MyAnimatedRectangle"
              Storyboard.TargetProperty="Opacity"
              From="1.0" To="0.0" Duration="0:0:5" AutoReverse="True" RepeatBehavior="Forever" />
          </Storyboard>
        </BeginStoryboard>
      </EventTrigger>
    </Rectangle.Triggers>
  </Rectangle>
</Canvas>