在DoubleAnimation之后,SetValue方法不起作用

时间:2015-04-17 15:27:29

标签: c# wpf canvas setvalue doubleanimation

我有这段代码:

public partial class MainWindow : Window
{

    Storyboard st_common = new Storyboard(); 
    DoubleAnimation anim1 = new DoubleAnimation();         

    public MainWindow()
    {
        InitializeComponent();               
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {

        anim1.Duration = new Duration(TimeSpan.FromSeconds(5));
        anim1.From = 10.0;
        anim1.To = 100.0;
        st_common.Children.Add(anim1);
        Storyboard.SetTargetName(anim1, r1.Name);
        Storyboard.SetTargetProperty(anim1, new PropertyPath(Canvas.TopProperty));
        st_common.Begin(this);

    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        r1.SetValue(Canvas.TopProperty, 300.0);            
        r2.SetValue(Canvas.TopProperty, 300.0);
    }
}

为什么SetValue方法在动画后对r1元素不起作用? (r1和r2是矩形)

1 个答案:

答案 0 :(得分:0)

故事板动画仍然保持最后一个动画值的值。这是一个已知的"功能",有三种方法...

  1. 将动画FillBehavior设置为停止
  2. 删除故事板
  3. 删除整个动画。
  4. 请参阅https://msdn.microsoft.com/en-us/library/aa970493%28v=vs.110%29.aspx了解如何执行这些操作。