单击后更改 wpf C# 中按钮的颜色,2 分钟后保留原始颜色 [英] change color of button in wpf C# after click and after 2 minutes retain the original color

查看:105
本文介绍了单击后更改 wpf C# 中按钮的颜色,2 分钟后保留原始颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码

        Hello.Background = System.Windows.Media.Brushes.Blue;
        var dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Interval = TimeSpan.FromSeconds(61);
        TimeSpan span = new TimeSpan(0,1,0);
        dispatcherTimer.Start(); 
        dispatcherTimer.Tick += delegate
        {

            if (dispatcherTimer.Interval > span)
            {
                Hello.Background = System.Windows.Media.Brushes.Red;
                dispatcherTimer.Stop();
            }
        };

但是按钮一直淡入淡出.我希望颜色保持不变

But button keeps fade in and fade out. i want color to be constant

            private void Button_Click(object sender, RoutedEventArgs e)
    {
        Hello.Background = System.Windows.Media.Brushes.Blue;
        var dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Interval = TimeSpan.FromSeconds(61);
        TimeSpan span = new TimeSpan(0,1,0);
        dispatcherTimer.Start(); 
        dispatcherTimer.Tick += delegate
        {

            if (dispatcherTimer.Interval > span)
            {
                Hello.Background = System.Windows.Media.Brushes.Red;
                dispatcherTimer.Stop();
            }
        };


    }

Xml

<Button Name="Hello" Content="Hello" Background="White"  Foreground="Black " Click="Button_Click">
 </Button>

推荐答案

你可以只创建一个 Style 并使用 Trigger 来启动一个 StoryboardColorAnimations

You could just create a Style and use a Trigger to start a Storyboard with ColorAnimations

示例:

<Style x:Key="AnimatedButton" TargetType="Button">
    <Setter Property="Background" Value="Red" />
    <Style.Triggers>
        <Trigger Property="IsPressed" Value="True">
            <Trigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard Storyboard.TargetProperty="Background.Color">
                        <ColorAnimation To="Blue" Duration="0:0:4" />
                        <ColorAnimation To="Red" BeginTime="0:1:52" Duration="0:0:4" />
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>
        </Trigger>
    </Style.Triggers>
</Style>

这篇关于单击后更改 wpf C# 中按钮的颜色,2 分钟后保留原始颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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