制作动画';风格';在WPF中使用ObjectAnimationUsingKeyFrames的控件的属性
我正在尝试使用ObjectAnimationUsingKeyFrames设置“Style”属性的动画。当我运行下面的示例时,我只看到一个空窗口,没有任何异常 几乎相同的示例适用于Silverlight。在WPF中,如果我直接指定控件的“Style”属性,它也可以工作。有人知道是否可以在WPF中设置“Style”属性的动画吗 非常感谢制作动画';风格';在WPF中使用ObjectAnimationUsingKeyFrames的控件的属性,wpf,controls,styles,animated,Wpf,Controls,Styles,Animated,我正在尝试使用ObjectAnimationUsingKeyFrames设置“Style”属性的动画。当我运行下面的示例时,我只看到一个空窗口,没有任何异常 几乎相同的示例适用于Silverlight。在WPF中,如果我直接指定控件的“Style”属性,它也可以工作。有人知道是否可以在WPF中设置“Style”属性的动画吗 非常感谢 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsof
<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>
当
ObjectAnimationUsingKeyFrames
尝试将动画设置为从DependencyObject
派生的值时,它会首先尝试设置对象。如果无法冻结对象,则会引发异常,动画不会运行
如果要为编写的自定义类型的值设置动画,则似乎需要从Freezable
派生或不从DependencyObject
派生
对于已经存在的从DependencyObject
派生而不是Freezable
的属性,您不能对它们设置动画(StyleProperty
或TemplateProperty
)。尝试在样式内部使用属性设置器:
<Style.Triggers>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Template" Value="{StaticResource TestTemplate}"/>
</Trigger>
</Style.Triggers>
将所有转换逻辑构建到样式中,而不是在不同样式之间切换。您可能面临的一个挑战是,目标属性必须是依赖项属性,因此不能使用IsLoaded
我希望你觉得这有用
最后一个想法是:定义是可能的,尽管我自己没有这样做。有一个外部机会,您可以编写自己的自定义“ObjectAnimation”,它不限于
Freezable
或非DependencyObject
类。如果您提供有关为什么要制作样式动画的更多信息,我可以建议其他替代方案。虽然将动画设置为不同的样式似乎很好,但我相信还有其他方法可以达到所需的效果。请尝试使用DynamicSource而不是StaticResource。