c# – 无法以编程方式设置WPF中的动画控件的宽度或高度(甚至不能通过Snoop)

这可能与动画有关.我有一个
WPF控件,其宽度和高度我通过几个不同的故事板动画.我创建了故事板,然后在它们上面调用Begin().我已经提供了如下故事板的代码.

我想重新评估一些事件(例如窗口调整大小)的控件大小,以便它与动画的值不同.我试图通过设置我的控件的宽度和高度属性手动(在动画运行后)在SizeChanged上处理此问题.调试器显示未设置这些值(保留原始值).

当我通过Snoop检查WPF时,Width和Height行以桃色/橙色突出显示,并尝试再次设置它的值不再持续它(当我聚焦时显示原始值.我的猜测是我的动画以某种方式覆盖了对属性的手动更改,但我不确定这是真的还是如何解决它.

故事板课

public class MyAnimationClass
{
    private Storyboard _myStoryboard;
    private DoubleAnimation _animation1;
    private DoubleAnimation _animation2;
    private DoubleAnimation _animation3;

    private void InitializeStoryboard()
    {
        _myStoryboard = CreateMyStoryboard(out _animation1, out _animation2, out _animation3);
    }

    private Storyboard CreateMyStoryboard(out DoubleAnimation animation1, out DoubleAnimation animation2, out DoubleAnimation animation3)
    {
        var myStoryboard = new Storyboard();

        // create animations
        animation1 = new DoubleAnimation { Duration = new TimeSpan(0, 0, 0, 0, 250), From = 0, To = 0 };
        animation2 = new DoubleAnimation { BeginTime = new TimeSpan(), Duration = new TimeSpan(0, 0, 0, 0, 250), From = 35, To = 35 };
        animation3 = new DoubleAnimation { BeginTime = new TimeSpan(0, 0, 0, 0, 250), Duration = new TimeSpan(0, 0, 0, 0, 250), From = 35, To = 0 };

        Storyboard.SetTargetProperty(animation1, new PropertyPath(FrameworkElement.WidthProperty));
        Storyboard.SetTargetProperty(animation2, new PropertyPath(FrameworkElement.HeightProperty));
        Storyboard.SetTargetProperty(animation3, new PropertyPath(FrameworkElement.HeightProperty));

        myStoryboard.Children.Add(animation1);
        myStoryboard.Children.Add(animation2);
        myStoryboard.Children.Add(animation3);

        return myStoryboard;
    }

    public void Animate(Control controlToAnimate)
    {
        // ....

        var finalWidth = CalculateFinalWidth();
        var finalHeight = CalculateFinalHeight();

        _animation1.To = finalWidth;
        _animation3.To = finalHeight;
        _myStoryboard.Begin(controlToAnimate);
    }
}

当我想要动画时,我在MyAnimationClass类的实例上调用Animate().

思考?

最佳答案 这最终成为一个非常简单的修复.由于FillBehavior控制了属性值,因此值没有变化.然而,简单地将其更改为FillBehavior.Stop并没有解决我的问题,因为当我的非动画代码设置宽度/高度时,下次我的动画运行它会动画到我想要的宽度/高度然后默认回到集合高度.这是通过在计算之后和动画之前设置控件的宽度/高度来解决的:

public void Animate(Control controlToAnimate)
{
    // ....

    var finalWidth = CalculateFinalWidth();
    var finalHeight = CalculateFinalHeight();

    // set values here so after animation they stay
    controlToAnimate.Width = finalWidth;
    controlToAnimate.Height = finalHeight;

    _animation1.To = finalWidth;
    _animation3.To = finalHeight;
    _myStoryboard.Begin(controlToAnimate);
}
点赞