【问题标题】:animation and data bindings动画和数据绑定
【发布时间】:2014-07-10 20:16:47
【问题描述】:

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

<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();

提前致谢! ;)

【问题讨论】:

  • 显示 C# 类,而不仅仅是代码行。
  • 共享 linea1 的属性声明? StackPanel 的 DataContext 是什么?

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


【解决方案1】:

对于 DataBinding,您只需要使用 Property。您不能使用 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 事件上开始动画。

【讨论】:

  • 你救了我!非常感谢! :D :D
猜你喜欢
  • 2016-04-02
  • 1970-01-01
  • 1970-01-01
  • 2011-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-22
相关资源
最近更新 更多