在Silverlight中,是否可以在Storyboard中使用TemplateBinding?

4

我正在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 解决方案会更加干净简洁。)

1个回答

0

请查看Expression Blend Samples,作为您问题的可能解决方案。您可以在ControlTemplate中使用许多交互类来创建所需的效果。文档不是很好,但对象浏览器中的描述应该会给您一些更多的线索 :)

例如,我有一个ListBox ItemTemplate,其中包含一个ControlStoryboardAction行为。此行为的触发器是DataTrigger,当DataContext字段包含特定值时触发(在我的情况下为Severity=="High")。然后,触发器在ItemTemplate内播放一个Storyboard。

<i:Interaction.Triggers>                                
<is:DataTrigger Binding="{Binding Severity, Mode=OneWay}" Value="High">
    <im:ControlStoryboardAction Storyboard="{StaticResource flashLight}" IsEnabled="True" />
</is:DataTrigger>

以下命名空间被引用:
  1. <i: - System.Windows.Interactivity
  2. <is: - Expression.Samples.Interactivity(可从上面的链接获取。我正在使用SL3的2009年7月发布版本)
  3. <im: - Microsoft.Expression.Interactivity.Media

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接