Comment : déclencher une animation en cas de modification d'une valeur de propriété

Cet exemple montre comment utiliser un Trigger pour démarrer un Storyboard moment où une valeur de propriété change. Vous pouvez utiliser un Trigger élément à l’intérieur d’un Style, ControlTemplateou DataTemplate.

Exemple

L’exemple suivant utilise un Trigger pour animer l’un Opacity d’entre elles Button lorsque sa IsMouseOver propriété devient 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>

Les animations appliquées par les objets de propriété Trigger se comportent de manière plus complexe que EventTrigger les animations ou les animations démarrées à l’aide Storyboard de méthodes. Ils « transfert » avec des animations définies par d’autres Trigger objets, mais composent avec et des EventTrigger animations déclenchées par des méthodes.

Voir aussi