【问题标题】:Why Storyboard value wouldn't get from a resource or binding?为什么 Storyboard 价值不会从资源或绑定中获得?
【发布时间】:2011-03-25 19:29:47
【问题描述】:

我在下面有结构,它可以工作:

 <Storyboard x:Key="GrowOnStart">
   <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Canvas.Left)" Storyboard.TargetName="window">
            <EasingDoubleKeyFrame KeyTime="0" Value="1024"/>

如果我尝试这样的事情,为什么它不起作用:

  <EasingDoubleKeyFrame KeyTime="0" Value="{DynamicResource StartingPositionLeft}"/>

是的,我在故事板之前定义了资源。 像下一个这样的声明也不起作用:

 <EasingDoubleKeyFrame KeyTime="0" Value="{Binding StartingPositionLeft}"/>

是的,它是代码背后的公共属性,并且 this.DataContext 设置为 this

【问题讨论】:

    标签: wpf binding resources storyboard dynamicresource


    【解决方案1】:

    这是因为动画是可冻结的对象。 MSDN Documentation 中有更多信息,但基本上这意味着您不能使用绑定,因为冻结对象(即动画)中的属性无法更改。

    要解决此限制,您需要在代码隐藏中完成部分或全部工作。

    【讨论】:

    • 是的,似乎是在代码后面编辑故事板的唯一方法。 var sb = (Storyboard)Resources["GrowOnStart"]; var anim = (DoubleAnimationUsingKeyFrames)sb.Children[0]; anim.KeyFrames[0].Value = 100;
    • 在后面的代码中,您应该能够获取您想要绑定到的值(而不是执行硬编码值)。例如,使用您的示例,您可以执行类似 anim.KeyFrames[0].Value = this.StartingPositionLeft (或您要绑定的任何属性)之类的操作,或者您可以使用 FindResource 查找并获取任何 DynamicResource 的值(或StaticResource),你想最初使用。
    猜你喜欢
    • 1970-01-01
    • 2019-03-15
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多