Do something before the visibilty is changed in wpf

两盒软妹~` 提交于 2020-01-06 14:39:53

问题


i'm trying to do an animation before the visibility changes to collapsed.

I'm trying to accomplish a fade in/fade out effect when the visibility is changed. I have no problem with fade in, because the visibility changes before my animation (which is good).

Here's my code right now:

private void LoginOverlay_OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    if (this.Visibility == Visibility.Visible)
    {
        //Fade in ... this is working
        Storyboard sb = new Storyboard();
        DoubleAnimation da = new DoubleAnimation();
        da.From = 0;
        da.To = 1;
        da.Duration = new Duration(TimeSpan.FromSeconds(1));
        sb.Children.Add(da);
        Storyboard.SetTargetProperty(da, new PropertyPath(OpacityProperty));
        Storyboard.SetTarget(da, this);
        sb.Begin();
    }
    else
    {
        //Fade out ... not working
        Storyboard sb = new Storyboard();
        DoubleAnimation da = new DoubleAnimation();
        da.From = 1;
        da.To = 0;
        da.Duration = new Duration(TimeSpan.FromSeconds(1));
        sb.Children.Add(da);
        Storyboard.SetTargetProperty(da, new PropertyPath(OpacityProperty));
        Storyboard.SetTarget(da, this);
        sb.Begin();
    }
}

回答1:


Before you start the Animation the visibility of the control has already changed to Collapsed or Hidden, this means that the opacity will animate but you won't be able to see it happen as the control is invisible.

One option you have is to change the control back to visible before starting the animation, then add a keyframe animation to the storyboard to set the visibility back to its intended value after 1 second (or the length of your fade animation)



来源:https://stackoverflow.com/questions/26932888/do-something-before-the-visibilty-is-changed-in-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!