4
我目前有一組按鈕,我想設置觸發器以便每個按鈕都執行相同的動畫。 XAML中有沒有方法將目標傳遞給故事板,以便我不必每次都爲每個目標重寫故事板?WPF在XAML中設置故事板目標
我目前有一組按鈕,我想設置觸發器以便每個按鈕都執行相同的動畫。 XAML中有沒有方法將目標傳遞給故事板,以便我不必每次都爲每個目標重寫故事板?WPF在XAML中設置故事板目標
如果您沒有設置明確的目標,則目標應該是是要應用動畫的元素。我將使用觸發器/動畫定義樣式,並將樣式應用於您想要表現此行爲的特定按鈕。例如:
<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
<Style.Resources>
<Storyboard x:Key="OnMouseEnterStoryboard">
<DoubleAnimation BeginTime="00:00:00" Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)" To="180" />
</Storyboard>
<Storyboard x:Key="OnMouseLeaveStoryboard">
<DoubleAnimation BeginTime="00:00:00" Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)" To="0" />
</Storyboard>
</Style.Resources>
<Style.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseLeave">
<RemoveStoryboard BeginStoryboardName="OnMouseEnterStoryboard_BeginStoryboard"/>
<BeginStoryboard x:Name="OnMouseLeaveStoryboard_BeginStoryboard" Storyboard="{StaticResource OnMouseLeaveStoryboard}"/>
</EventTrigger>
<EventTrigger RoutedEvent="Mouse.MouseEnter">
<BeginStoryboard x:Name="OnMouseEnterStoryboard_BeginStoryboard" Storyboard="{StaticResource OnMouseEnterStoryboard}"/>
<RemoveStoryboard BeginStoryboardName="OnMouseLeaveStoryboard_BeginStoryboard"/>
</EventTrigger>
</Style.Triggers>
<Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
<Setter Property="RenderTransform">
<Setter.Value>
<RotateTransform/>
</Setter.Value>
</Setter>
</Style>
,然後在每一個按鈕,你想這樣的行爲:
<Button Style="{StaticResource MyButtonStyle}" ... />
真棒!我剛開始學習WPF,並沒有想到應用關聯的觸發器和動畫的樣式。謝謝您的幫助! – jwarzech 2009-10-29 17:17:36