简体   繁体   English

将属性绑定到XAML中的Storyboard值

[英]Binding property to Storyboard value in XAML

I have a Storyboard animation that looks like this: 我有一个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>

The initial value I set in the properties InitialWidth and FinalWidth seems to be correct, but if I change later on, they are not updated and are 0 . 我在属性InitialWidthFinalWidth设置的初始值似乎是正确的,但是如果以后更改,它们将不会更新,并且为0 I read somewhere that it's not possible to bind properties to Storyboard values at runtime, but I did not find any official document stating that. 我在某处读到,无法在运行时将属性绑定到Storyboard值,但是我没有找到任何官方文件说明这一点。
My question is, can I bind properties to Storyboard values? 我的问题是,我可以将属性绑定到Storyboard值吗? If so, what do I need to change? 如果是这样,我需要更改什么?

I can't test this, so just take it as a suggestion for something that "might work". 我无法对此进行测试,因此只能将其作为“可能起作用”的建议。 The idea is to create a "proxy" object in your page resources, and then reference it using a StaticResource binding. 这个想法是在页面资源中创建一个“代理”对象,然后使用StaticResource绑定对其进行引用。

The proxy is just a DependencyObject that contains the properties you're interested in binding: 代理只是一个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));
}

Now instantiate it in your page resources, with bindings to your view model: 现在在页面资源中实例化它,并绑定到您的视图模型:

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

Then you can reference this as a static resource from the timeline elements: 然后,您可以从时间轴元素中将其作为静态资源引用:

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

I have successfully used something similar in a Silverlight (visual state storyboards in a control template), but I'm not sure if it will apply to your scenario. 我已经在Silverlight中成功使用了类似的东西(控件模板中的可视状态情节提要),但是我不确定它是否适用于您的方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM