厚度动画有问题吗? (带有Begin函数参数)

时间:2011-12-15 15:33:33

标签: c# wpf animation

主框架上有按钮和控件以及堆叠面板。试图将工作动画从xaml移动到.cs我已经编写了下一个很酷的函数)):

private void ToggleButton_Checked(object sender, RoutedEventArgs e)
        {
int heightBottom = 100;

System.Windows.Thickness th = stackPanel1.Margin;
            th.Top = stackPanel1.Margin.Top + heightBottom;
ThicknessAnimation animStackPanel1 = new ThicknessAnimation
            {
                From = stackPanel1.Margin,
                To = th,
                AccelerationRatio = 0.2,
                FillBehavior = FillBehavior.Stop,
                DecelerationRatio = 0.8,
                Duration = DURATION
            };
System.Windows.Thickness th2 = fxPSEditorView.Margin;
            th2.Right = fxPSEditorView.Margin.Right - heightBottom;
            th2.Bottom = fxPSEditorView.Margin.Bottom - heightBottom;

            ThicknessAnimation animPSEditorView = new ThicknessAnimation
            {
                From = fxPSEditorView.Margin,
                To = th2,
                AccelerationRatio = 0.2,
                FillBehavior = FillBehavior.Stop,
                DecelerationRatio = 0.8,
                Duration = DURATION
            };
Storyboard.SetTarget(animPSEditorView, fxPSEditorView);
            Storyboard.SetTargetProperty(fxPSEditorView, new PropertyPath(MarginProperty));
            sb.Children.Add(animPSEditorView);
Storyboard.SetTarget(animStackPanel1, stackPanel1);
            Storyboard.SetTargetProperty(stackPanel1, new PropertyPath(MarginProperty));
            sb.Children.Add(animStackPanel1);
sb.Begin();//null reference error here! 
};

据我所知,我必须为sb.Begin指定两个参数 - 一个用于stackPanel1,另一个用于fxPSEditorView。但它不需要一组对象作为第一个参数。  如何运行这个动画的任何想法将是好的!谢谢

1 个答案:

答案 0 :(得分:1)

您不需要两个参数,但是您应该传递包含动画控件的控件(需要在同一个名称范围内)。阅读Begin(FrameworkElement)的文档。