views:

34

answers:

1

I have a StackPanel from which I want to remove an element. Prior to removing the element, I want to perform an animation on the element to signal the element is about to be removed. However, performing the animation followed up by removing the element causes the element to remove immediately, with no visible animation. Is there a correct way to do this?

StackPanel myStackPanel = new StackPanel();
// myStackPanel is loaded with visible elements here.

// Time to remove an element!
MyUserControl control = myStackPanel.Children[0] as MyUserControl;
control.SomeAnimation.Begin();
myStackPanel.Children.Remove(control);

UPDATE I ended up doing the following ugliness to get this working as hoped:

control.SomeAnimation.Begin();
Thread t = new Thread(delegate()
{
   Thread.Sleep(500);
   Dispatcher.BeginInvoke( () => myStackPanel.Children.Remove(control));
});
t.Start();
+1  A: 

You could use something like the Completed event (I am sure there are other ways for getting the same result).

    private void SomeAnimation_Completed(object sender, EventArgs e)
    {
        //Then call your remove code or method.
    }

Oddly enough, Blend 3 doesn't show Storyboard.Completed as an event though intellesense, though VS 2008 does.

That's a good suggestion. For my purposes, however, the animation would trigger as part of a public call to MyUserControl. Something akin to MyUserControl.FadeOut() which would animate the fade-out effect, but the hosting StackPanel is not accessible from MyStackPanel.FadeOut() method.
David