淡出使用WPF动画任何控制 [英] Fade any control using a WPF animation

查看:162
本文介绍了淡出使用WPF动画任何控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想切换控制(按钮,文本框,面板等)的不透明度在我的WPF项目,并要检查,看看我是否正确地做到了。

I want to toggle the opacity of a control (Button, TextBox, Panel, etc) in my WPF project and wanted to check to see if I had done it correctly.

我的问题是:这是的功能,你通常写在XAML或者你会使用code类似于下面实现淡入/淡出结果的类型?

My question is: Is this the type of functionality that you’d normally write in XAML or would you use code similar to that below to achieve the fade in/fade out result?

internal static class AnimationExtensions
{
    internal enum TransitionSpeed
    {
        Instant = 0,
        Fast = 100,
        Normal = 200,
        Slow = 500
    }

    /// <summary>
    /// Toggles the opacity of a control.
    /// </summary>
    /// <param name="control">The control.</param>
    internal static void ToggleControlFade(this Control control)
    {
        control.ToggleControlFade(TransitionSpeed.Normal);
    }

    /// <summary>
    /// Toggles the opacity of a control.
    /// </summary>
    /// <param name="control">The control.</param>
    /// <param name="speed">The speed.</param>
    internal static void ToggleControlFade(this Control control, TransitionSpeed speed)
    {
        Storyboard storyboard = new Storyboard();
        TimeSpan duration = new TimeSpan(0, 0, 0, 0, (int)speed); //

        DoubleAnimation animation = new DoubleAnimation { From = 1.0, To = 0.0, Duration = new Duration(duration) };
        if (control.Opacity == 0.0)
        {
            animation = new DoubleAnimation { From = 0.0, To = 1.0, Duration = new Duration(duration) };
        }

        Storyboard.SetTargetName(animation, control.Name);
        Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity", 0));
        storyboard.Children.Add(animation);

        storyboard.Begin(control);
    }
}

正如你可能会说,我非常,非常新的WPF。

As you can probably tell I’m very, very new to WPF.

感谢

推荐答案

我倾向于认为在我需要一个动画或者动画是依赖于复杂的后要执行的操作的地方触发了code-背后是最好的地方,否则XAML是一个很好的地方放动画。 (我通常做喜欢的事情转换或简单的的onclick之类的事件。

I tend to find in places where I need to perform an action after an animation or where the animation is dependent on complex triggers that the code-behind is the best place, otherwise the XAML is a good place to put the animation. (I usually do this for things like transitions or simple 'onclick' sort of events.

这篇关于淡出使用WPF动画任何控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆