3
votes

I have a scenario where I would like to animate a WPF control right before it is removed from the visual/logical trees. The control is custom, and the control style, including animations would be supplied by a designer (i.e. I don't know the details of what they will be at the time of writing the control code). I would like to provide routed events that allow the designer to trigger an animation right before the object is removed (e.g. the user is dragging the control with the mouse, and when the mouse button is released the control might fade away slowly instead of ubruptly being removed from the visual/logical tree). Is this possible just using routed events? Is it possible to detect when the animation has completed without requiring the designer to set a quirky dependency property or at the end of the animation to trigger the removal of the control? Using a dependency property would create a memory leak if the designer failed to set the property. Is there a better way?

1

1 Answers

2
votes

Every storyboard has a Completed event that is called when the animation is done. You should handle that event and delete the control from that event. That way you can just start the animation when the mouse button is released.