Getting a UIElement from a completed animation event

From my code, I want to start an animation on a specific UIElement , when this animation ends, I would like to do some other processing on this UIElement . I find it difficult to understand how to convert the AnimationClock object that I receive as the sender of the Animation Completed event to the UIElement object in which the animation was performed.

Here is the code I use to create and run the animation:

 DoubleAnimation FadeOutAnim = new DoubleAnimation(1, 0, TimeSpan.FromSeconds(.5)); FadeOutAnim.Completed += new EventHandler(FadeOutAnim_Completed); UIElement element = lstMessages.ItemContainerGenerator.ContainerFromItem(sender) as UIElement; if(element != null) element.BeginAnimation(UIElement.OpacityProperty, FadeOutAnim); 

And here is my Completed event, in which I want to access UIElement again.

 void FadeOutAnim_Completed(object sender, EventArgs e) { UIElement animation = sender; //This is an AnimationClock and I can't seem to figure out how to get my UIElement back. } 

Any help would be greatly appreciated.

+4
source share
1 answer

If the handler is useless (I cannot find a way to return the animated element), you can simply raise another event that contains this information:

 private event EventHandler FadeAnimationCompleted; private void OnFadeAnimationCompleted(object sender) { var handler = FadeAnimationCompleted; if (handler != null) handler(sender, null); } 
 FadeAnimationCompleted += new EventHandler(This_FadeAnimationCompleted); FadeOutAnim.Completed += (s, _) => OnAnimationCompleted(element); 
 void This_FadeAnimationCompleted(object sender, EventArgs e) { //Sender is the UIElement } 

It would be even simpler to make a direct call method in the delegate:

 FadeOutAnim.Completed += (s, _) => FadeAnimationCompleted(element); 
 void FadeAnimationCompleted(UIElement element) { //Meaningful code goes here. } 
+6
source

All Articles