Dela via


Anvisningar: Utlösa en animering när ett egenskapsvärde ändras

Det här exemplet visar hur du använder en Trigger för att starta en Storyboard när ett egenskapsvärde ändras. Du kan använda en Trigger i en Style, ControlTemplateeller DataTemplate.

Exempel

I följande exempel används en Trigger för att animera Opacity för en Button när dess egenskap IsMouseOver blir true.

<!-- PropertyTriggerExample.xaml
     Shows how to use property triggers to start animations. -->
<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  WindowTitle="Animate Properties with Storyboards">
  <Page.Resources>
  
    <Style x:Key="PropertyTriggerExampleButtonStyle" TargetType="{x:Type Button}">
      
      <Setter Property="Opacity" Value="0.25" />
      
      <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">

          <Trigger.EnterActions>
            <BeginStoryboard>
              <Storyboard>
                <DoubleAnimation Storyboard.TargetProperty="Opacity"
                  To="1" Duration="0:0:1" />
              </Storyboard>
            </BeginStoryboard>
          </Trigger.EnterActions>
          <Trigger.ExitActions>
            <BeginStoryboard>
              <Storyboard>
                <DoubleAnimation Storyboard.TargetProperty="Opacity"
                  To="0.25" Duration="0:0:1" />
              </Storyboard>
            </BeginStoryboard>
          </Trigger.ExitActions>          
        </Trigger>               
      </Style.Triggers>    
    </Style>
  </Page.Resources>

  <StackPanel Margin="20">

    <Button Style="{StaticResource PropertyTriggerExampleButtonStyle}">
      Move the mouse over me.
    </Button>

  </StackPanel>
</Page>

Animeringar som tillämpas på egenskap Trigger-objekt fungerar på ett komplexare sätt än EventTrigger-animeringar eller animeringar som startats med hjälp av Storyboard-metoder. De "överlämnar" med animeringar som definierats av andra Trigger-objekt, men kombineras med EventTrigger och metodutlösta animeringar.

Se även