views:

58

answers:

1

I am making a button invisible once it gets clicked. Is there any nice animation (programmably from code behind) code that fade the button away instead of sudden disappearance?

+2  A: 

This should help you. Just call FadeOut(myButton) :

    private void FadeOut(UIElement fe, int seconds = 2)
    {
        DoubleAnimation animation = new DoubleAnimation() { To = 0, Duration = new Duration(new TimeSpan(0, 0, seconds)) };
        Storyboard sb = new Storyboard();
        sb.Children.Add(animation);
        Storyboard.SetTarget(animation, fe);
        Storyboard.SetTargetProperty(animation, new PropertyPath(UIElement.OpacityProperty));
        sb.Begin();
    }
Schemer
For belts and braces it would be better to set the Visibility to Collapsed once the animation is complete.
AnthonyWJones
Good idea. But sometimes with Grid based layouts, depending on where the button is positioned, it might make the form go wonky if the button is suddenly collapsed. In any case, I would never go the code-behind / programming route in a case like this... XAML keeps things simple.
Schemer