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

发布于 2024-08-02 22:57:25 字数 1327 浏览 4 评论 0 原文

我正在 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 -->

我在自定义控制代码中触发动画,但是当动画开始时,值看起来并没有更新。我只是想知道我是否遗漏了某些内容,或者是否可以将 TemplateBinding 应用于我的 ControlTemplate 中的资源。

(我目前正在使用手动将背景分配给 EasingColorKeyFrame 值的解决方法,但 TemplateBinding 解决方案会干净得多。)

I'm building a custom control in Silverlight and I want one of the fields to animate to the value of a DependencyProperty when that property is changed. More specifically, I have particular item in my Control Template that I want to animate to the color of the Background whenever the background changes color. So, what I have is:

<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 -->

I'm triggering the animation in the custom control code, but when the animation starts, it doesn't look like the Value is updating. I was just wondering if I'm missing something or if it is at all possible to apply TemplateBinding to resources in my ControlTemplate.

(I'm currently using a work-around of manually assigning the Background to the EasingColorKeyFrame Value, but the TemplateBinding solution would be so much cleaner.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落画卷 2024-08-09 22:57:25

查看Expression Blend 示例 作为您问题的可能解决方案。您可以在 ControlTemplate 中使用许多交互类来创建您想要的效果。该文档不是很好,但对象浏览器中的描述应该为您提供更多线索:)

例如,我有一个包含 ControlStoryboardAction 行为的 ListBox ItemTemplate。此行为的触发器是 DataTrigger,当 DataContext 字段包含特定值时会触发该触发器。 (在我的情况下,当严重性==“高”)触发器然后在 ItemTemplate 中播放故事板。

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

引用了以下命名空间:

  1. - System.Windows.Interactivity
  2. - Expression.Samples.Interactivity (可从上面的链接获取。我正在使用2009 年 7 月发布的 SL3)
  3. - Microsoft.Expression.Interactivity.Media

Have a look at Expression Blend Samples as a possible solution to your problem. There are a number of Interactivity classes that you could use within your ControlTemplate to create the effect your looking for. The documentation is not great, but descriptions in the Object Browser should give you some more clues :)

For example, I have a ListBox ItemTemplate that contains a ControlStoryboardAction Behaviour. The trigger for this Behaviour is a DataTrigger which fires when a DataContext field contains a specific value. (In my case when Severity=="High") The trigger then Plays a Storyboard within the ItemTemplate.

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

The following namespaces are referenced:

  1. <i: - System.Windows.Interactivity
  2. <is: - Expression.Samples.Interactivity (available from the link above. I am using the July 2009 release for SL3)
  3. <im: - Microsoft.Expression.Interactivity.Media
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文