我正在嘗試使用ObjectAnimationUsingKeyFrames爲'樣式'屬性創建動畫。當我運行下面的示例時,我只看到空的窗口,沒有任何異常。在WPF中使用ObjectAnimationUsingKeyFrames控制動畫的「樣式」屬性
幾乎相同的示例在Silverlight中工作。在WPF中,它也可以工作,如果我直接分配控件的'Style'屬性。有誰知道是否可以在WPF中爲'Style'屬性設置動畫?
非常感謝。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:this="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525"
>
<Window.Resources>
<ResourceDictionary>
<Style x:Key="TestStyle" TargetType="Control">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Canvas x:Name="Rectangle">
<Rectangle Width="200" Height="150" Fill="Red"/>
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Window.Resources>
<Canvas>
<Canvas.Triggers>
<EventTrigger RoutedEvent="Canvas.Loaded">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="Target" Storyboard.TargetProperty="Style" >
<DiscreteObjectKeyFrame KeyTime="0:0:0.0" Value="{StaticResource ResourceKey=TestStyle}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Canvas.Triggers>
<Canvas.Children>
<ContentControl x:Name="Target"/>
</Canvas.Children>
</Canvas>
嘗試DynamicResource而不是StaticResource。 – Bijan 2012-10-10 15:30:55