remove animation completed event from an element in WPF code?

44 views Asked by At

As shown below, I have a group of elements which I animate separately with two DoubleAnimations targeting opacity. I then listen to the animation completed events so that they cycle until I interrupt the cycle individually.

for(UIElement myElement in ListOfElements)
{
    DoubleAnimation fadeIn = new DoubleAnimation(0, 1, TimeSpan.FromSeconds(fadeInSeconds));
    DoubleAnimation fadeOut = new DoubleAnimation(1, 0, TimeSpan.FromSeconds(fadeOutSeconds));
    fadeIn.Completed += (s, e) => 
        myElement.BeginAnimation(UIElement.OpacityProperty, fadeOut);
    fadeOut.Completed += (s, e) => 
        myElement.BeginAnimation(UIElement.OpacityProperty, fadeIn);
}

In some cases, I want to stop the cycle of animation for a specific element by removing the animation AND the listener for the fadeout.completed event. Stopping it with myElement.BeginAnimation(UIElement.OpacityProperty, null) works fine but it does not prevent the Completed event from firing at the time it would have completed. That is, stopping the animation does not prevent the .Completed event from firing. So to remove the Completed listener, I think must retrieve the "fadeout" animation for a specific element and then unsubscribe the listener with -=

First, how can I find the active animations for a given element, so that I can access it's Completed event.

Second, how to remove the listeners from that animation's completed event?

0

There are 0 answers