动画和数据绑定

时间:2014-07-10 20:16:48

标签: c# xaml animation windows-phone-8

我需要动画的帮助,我试图通过数据绑定按代码设置SplineDoubleKeyFrame的值,但它不起作用,为什么? 代码xaml:

<StackPanel Margin="0,435,0,0">
            <StackPanel.Resources>
                <Storyboard x:Name="myStoryboard">
                    <DoubleAnimationUsingKeyFrames  Storyboard.TargetName="barra" Storyboard.TargetProperty="Width">
                        <LinearDoubleKeyFrame Value="0" KeyTime="0:0:0" />

                        <SplineDoubleKeyFrame KeySpline="0,0 1,0" Value="{Binding linea1}" KeyTime="0:0:0.8" />

                        <SplineDoubleKeyFrame KeySpline="0.10, 0.21 0.00, 1.0" Value="{Binding linea1}"  KeyTime="0:0:1.5" />
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>
            </StackPanel.Resources>
            <Rectangle Fill="White" HorizontalAlignment="Left" Height="72" Margin="12,0,0,0" Grid.Row="1" Stroke="#FF8E76FF" VerticalAlignment="Top" Width="444" StrokeThickness="5"/>
            <Rectangle Visibility="Visible" x:Name="barra" Fill="#FF8E76FF" HorizontalAlignment="Left" Height="72" Margin="12,-72,0,0" Stroke="#FF8E76FF" StrokeThickness="5" VerticalAlignment="Top" Width="456"/>
        </StackPanel>

C#:

linea1 = 440;
myStoryboard.Begin();

提前致谢! ;)

1 个答案:

答案 0 :(得分:0)

对于 DataBinding ,您只需要使用属性。您不能使用varibale进行绑定(例如:var linea1 = 440;)

要创建属性,我创建一个类

  public class StackpanelProperties
{
    public int linea1 { get; set; }
}

并将datacontext设置为Stackpnael,以便我可以使用此属性进行绑定

this.InitializeComponent();
stack.DataContext = new StackpanelProperties() { linea1 = 440 };

xaml代码

<StackPanel Name="stack" Margin="0,435,0,0">
    <StackPanel.Triggers>
        <EventTrigger>
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimationUsingKeyFrames Duration="0:0:0.8" EnableDependentAnimation="True" Storyboard.TargetName="barra" Storyboard.TargetProperty="Width">
                        <LinearDoubleKeyFrame Value="0" KeyTime="0:0:0" />
                        <SplineDoubleKeyFrame KeySpline="0,0 1,0" Value="{Binding linea1}" KeyTime="0:0:0.8" />
                        <SplineDoubleKeyFrame KeySpline="0.10, 0.21 0.00, 1.0" Value="{Binding linea1}"  KeyTime="0:0:1.5" />
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </StackPanel.Triggers>
    <Rectangle Fill="White" HorizontalAlignment="Left" Height="72" Margin="12,0,0,0" Grid.Row="1" Stroke="#FF8E76FF" VerticalAlignment="Top" Width="444" StrokeThickness="5"/>
    <Rectangle Visibility="Visible" x:Name="barra" Fill="#FF8E76FF" HorizontalAlignment="Left" Height="72" Margin="12,-72,0,0" Stroke="#FF8E76FF" StrokeThickness="5" VerticalAlignment="Top" Width="456"/>
</StackPanel>

注意: EventTrigger用于stackpnael Loaded事件的开始动画。