在点击后更改wpf C#中的按钮颜色,并在2分钟后保留原始颜色

时间:2013-10-30 05:39:40

标签: c# wpf

我正在使用此代码

        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();
            }
        };

但按钮会逐渐淡入淡出。 我希望颜色不变

C#

            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();
            }
        };


    }

的Xaml

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

4 个答案:

答案 0 :(得分:4)

您可以创建一个Style并使用TriggerStoryboard

开始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>

答案 1 :(得分:1)

现在试试吧......不过这是代码背后的代码

XAML代码:

   <Button  Content="Button"  x:Name="MyButton" Height="23" HorizontalAlignment="Left" 
    Margin="94,128,0,0"  VerticalAlignment="Top" Width="75"/>

Cs档案

    private void StartAnimation()
    {

        Color fromRGB= Color.FromRgb(255, 255, 255); ;
        Color ToRGB= Color.FromRgb(255, 0, 0);

        SolidColorBrush myBrush = new SolidColorBrush();
        myBrush.Color = Colors.Black;
        ColorAnimation myAnimation = new ColorAnimation();
        myAnimation.From = fromRGB;
        myAnimation.To = ToRGB;
        myAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(120000));
        myAnimation.AutoReverse = true;

        myBrush.BeginAnimation(SolidColorBrush.ColorProperty, myAnimation );

        MyButton.Background = myBrush;
    }

您可以在调用事件时更改颜色,然后调用动画。

答案 2 :(得分:0)

你可以尝试使用在Blend中创建的简单StoryBoard然后应用于Button / style 像这样的东西:

<Button Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="75" >
        <Button.Background>
            <SolidColorBrush x:Name="MySolidColorBrush" Color="Brown" />
        </Button.Background>
        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.Click">
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation 
                            Storyboard.TargetName="MySolidColorBrush"
                            Storyboard.TargetProperty="Color"
                            From="Red" To="Yellow" Duration="0:0:0" RepeatBehavior="1x"  />
                        <ColorAnimation 
                            Storyboard.TargetName="MySolidColorBrush"
                            Storyboard.TargetProperty="Color"
                            From="Yellow" To="Blue" Duration="0:0:0" RepeatBehavior="1x" BeginTime="0:0:10"  />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
            </Button.Triggers>
    </Button>

答案 3 :(得分:0)

只是为了展示一种不同的方法......如果你正在使用MVVM,你可以将你的按钮颜色绑定到ViewModel上的一个属性,一旦你点击它,就运行你的2分钟背景/计时器。完成2分钟后,它会将颜色更改为另一个。

涉及的xaml不多,我确实喜欢其他一些解决方案:)

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    Thread.Sleep(2000); // two second
        ButtonColorPropertyName= Colors.Red;
}

(假设你有ButtonColorPropertyName或你想要命名的任何东西,在ViewModel中)