简体   繁体   English

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

[英]Cannot set width or height of animated control in WPF programmatically (not even through Snoop)

This may be animation related. 这可能与动画有关。 I have a WPF control whose width and height I'm animating through several different storyboards. 我有一个WPF控件,我正在通过几个不同的情节提要板设置动画的宽度和高度。 I create the storyboards and then call Begin() on them. 我创建情节提要,然后在它们上调用Begin()。 I've provided code for how the storyboards look below. 我提供了以下故事板外观的代码。

I want to re-evaluate the control size on some event (eg window resize) so that it is different from the value that was animated. 我想重新评估某个事件的控件大小(例如,窗口大小调整),以使其与动画值不同。 I attempt to handle this on SizeChanged manually (after the animation has run) by setting the Width and Height properties of my control. 我试图通过设置控件的WidthHeight属性来手动处理SizeChanged(在动画运行之后)。 The debugger shows that those values are not set (the original values remain). 调试器显示未设置这些值(保留原始值)。

When I inspect the WPF through Snoop, the Width and Height rows are highlighted in a peach/orange color and trying to set the values through it again does not persist it (the original value is shown when I focus away. My guess is that my animation is somehow overriding manual changes to the property but I'm not sure if this is true or how to solve it. 当我通过探听检查WPF时,“宽度”和“高度”行以桃红色/橙色突出显示,并且尝试再次通过其设置值并不能持久化(我将焦点移开时会显示原始值。我的猜测是动画在某种程度上超越了对属性的手动更改,但是我不确定这是否正确或如何解决。

Storyboard Class 故事板类

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

When I want to animate something, I call Animate() on the instance of my MyAnimationClass class. 当我想制作动画时,我在MyAnimationClass类的实例上调用Animate()

Thoughts? 有什么想法吗?

This ended up being a pretty simple fix. 最终这是一个非常简单的修复。 The value wasn't changing because of FillBehavior controlling the property value. 由于FillBehavior控制属性值, FillBehavior该值未更改。 However, simply changing it to FillBehavior.Stop didn't solve my problem because when my non-animation code set width/height, the next time my animation ran it would animate to the width/height I wanted and then default back to the set height. 但是,仅将其更改为FillBehavior.Stop并不能解决我的问题,因为当我的非动画代码设置宽度/高度时,下次我的动画运行时,它将动画为我想要的宽度/高度,然后默认返回到设置高度。 This was solved by setting the width/height of the control after calculation and before animation: 通过在计算之后和动画之前设置控件的宽度/高度可以解决此问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM