我正在Silverlight中构建一个自定义控件,当DependencyProperty的值发生改变时,我希望其中一个字段能够动画显示出该值。更具体地说,我想要在我的控件模板中的特定项目上实现这个功能,每当背景色发生变化时,这个项目就会以背景色为目标颜色进行动画效果展示。因此,我所拥有的是:
<ControlTemplate TargetType="local:MyType">
<Grid x:Name="PART_RootElement">
<Grid.Resources>
<Storyboard x:Name="PART_FillAnimation">
<ColorAnimationUsingKeyFrames
BeginTime="00:00:00"
Storyboard.TargetName="PART_MainPath"
Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
<EasingColorKeyFrame
x:Name="PATH_FillKeyframe"
KeyTime="00:00:01"
Value="{TemplateBinding Background}"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</Grid.Resources>
<!-- the rest of the template -->
我在自定义控件代码中触发动画,但当动画开始时,它似乎未更新 Value。我想知道是否有遗漏的部分或者是否有可能将 TemplateBinding 应用于 ControlTemplate 中的资源。
(目前我正在手动将 Background 分配给 EasingColorKeyFrame 的 Value ,但是 TemplateBinding 解决方案会更加干净简洁。)