簡體   English   中英

將屬性綁定到XAML中的Storyboard值

[英]Binding property to Storyboard value in XAML

我有一個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值嗎? 如果是這樣,我需要更改什么?

我無法對此進行測試,因此只能將其作為“可能起作用”的建議。 這個想法是在頁面資源中創建一個“代理”對象,然后使用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中成功使用了類似的東西(控件模板中的可視狀態情節提要),但是我不確定它是否適用於您的方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM