Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate an element where the trigger is a data change on the property you want to animate

I want to animate my WPF window. It's set to size to content, so when the content changes, the window changes size. I don't want it to snap to a new size though, when it determines it needs a new size I want it to lerp between the old and new value with an animation.

How do I set this up?

EDIT: I am using SizeToContent=WidthAndHeight" as a parameter for my window, and want to animate when it automatically sizes to different content being shown.

like image 666
pingu2k4 Avatar asked Jun 05 '19 11:06

pingu2k4


People also ask

How do you animate a trigger in PowerPoint?

On the Animations tab, in the Advanced Animation group, click Animation Pane. In the Animation Pane, select the animation that you want to trigger. In the Advanced Animation group, click Trigger, point to On Click of, and select the object that you want to trigger the animation.

What is a trigger in content of animation?

When you want to click a specific thing on a slide to start an animation effect, use a trigger. Triggers give you specific click points for controlling animation, and are especially useful when you want several effects on a slide. Trigger an animation effect to begin when you click it.

How do you animate an element in HTML?

The Element interface's animate() method is a shortcut method which creates a new Animation , applies it to the element, then plays the animation. It returns the created Animation object instance. Note: Elements can have multiple animations applied to them.


1 Answers

You may override the OnPropertyChanged method and start an animation of any property you like.

For example, the Width property:

private DoubleAnimation widthAnimation;

protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
    if (e.Property == WidthProperty &&
        !double.IsNaN((double)e.OldValue) &&
        widthAnimation == null)
    {
        widthAnimation = new DoubleAnimation
        {
            Duration = TimeSpan.FromSeconds(1),
            From = (double)e.OldValue,
            To = (double)e.NewValue
        };

        widthAnimation.Completed += (s, a) =>
        {
            widthAnimation = null;
            BeginAnimation(WidthProperty, null);
        };

        BeginAnimation(WidthProperty, widthAnimation);
    }
    else
    {
        base.OnPropertyChanged(e);
    }
}
like image 122
Clemens Avatar answered Oct 23 '22 21:10

Clemens