【问题标题】:Binding property to Storyboard value in XAML将属性绑定到 XAML 中的情节提要值
【发布时间】:2015-03-06 23:26:13
【问题描述】:

我有一个看起来像这样的Storyboard 动画:

<Grid Name="Choice" VerticalAlignment="Stretch" Width="400">
    <Interactivity:Interaction.Behaviors>
        <Core:EventTriggerBehavior EventName="Tapped">
            <Media:ControlStoryboardAction>
                <Media:ControlStoryboardAction.Storyboard>
                    <Storyboard x:Name="Animation">
                        <DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="Choice">
                            <EasingDoubleKeyFrame KeyTime="0" Value="{Binding Path=InitialWidth}"/>
                            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="{Binding Path=FinalWidth}" />
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                </Media:ControlStoryboardAction.Storyboard>
            </Media:ControlStoryboardAction>
        </Core:EventTriggerBehavior>
    </Interactivity:Interaction.Behaviors>
    <Image HorizontalAlignment="Center" VerticalAlignment="Center" Source="{Binding Path=Image}" Stretch="UniformToFill" />
</Grid>

我在属性InitialWidthFinalWidth 中设置的初始值似乎是正确的,但是如果我稍后更改,它们不会更新并且是0。我在某处读到无法在运行时将属性绑定到 Storyboard 值,但我没有找到任何官方文档说明这一点。
我的问题是,我可以将属性绑定到 Storyboard 值吗?如果是这样,我需要改变什么?

【问题讨论】:

  • 理论上可以,因为EasingDoubleKeyFrame.Value是一个依赖属性。棘手的部分是弄清楚如何,因为故事板不在视觉树中。您是否在输出窗口中看到任何绑定错误?
  • 不,没有绑定错误。

标签: wpf xaml binding storyboard windows-store-apps


【解决方案1】:

我无法对此进行测试,因此仅将其作为“可能有效”的建议。这个想法是在您的页面资源中创建一个“代理”对象,然后使用StaticResource 绑定来引用它。

代理只是一个 DependencyObject,其中包含您有兴趣绑定的属性:

public class ProxyObject : DependencyObject
{
    public double InitialWidth
    {
        get { return (double)GetValue(InitialWidthProperty); }
        set { SetValue(InitialWidthProperty, value); }
    }
    public static readonly DependencyProperty InitialWidthProperty =
        DependencyProperty.Register("InitialWidth", typeof(double), typeof(ProxyObject), new PropertyMetadata(0));

    public double FinalWidth
    {
        get { return (double)GetValue(FinalWidthProperty); }
        set { SetValue(FinalWidthProperty, value); }
    }
    public static readonly DependencyProperty FinalWidthProperty =
        DependencyProperty.Register("FinalWidth", typeof(double), typeof(ProxyObject), new PropertyMetadata(0));
}

现在在您的页面资源中实例化它,并绑定到您的视图模型:

<Grid Name="Choice">
    <Grid.Resources>
        <ResourceDictionary>
            <local:ProxyObject x:Name="proxy"
                InitialWidth="{Binding InitialWidth}" FinalWidth="{Binding FinalWidth}" />
        </ResourceDictionary>
    </Grid.Resources>
    ...
</Grid>

然后您可以将其作为时间轴元素中的静态资源引用:

<EasingDoubleKeyFrame KeyTime="0" 
    Value="{Binding Source="{StaticResource proxy},Path=InitialWidth}" />
<EasingDoubleKeyFrame KeyTime="0:0:1" 
    Value="{Binding Source="{StaticResource proxy},Path=InitialWidth}" />

我已经成功地在 Silverlight 中使用了类似的东西(控制模板中的视觉状态故事板),但我不确定它是否适用于您的场景。

【讨论】:

  • 这确实使我的代码中没有的更多工作。但是,它没有提供我想要的行为。我想可能是因为我有一个列表,每个项目都有自己的故事板,就像上面一样。我将不得不在一个更简单的情况下对此进行测试。
猜你喜欢
  • 1970-01-01
  • 2011-01-12
  • 2014-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-05
  • 1970-01-01
相关资源
最近更新 更多