change color of button in wpf C# after click and after 2 minutes retain the original color

后端 未结 4 1917
眼角桃花
眼角桃花 2021-01-22 10:33

I am using this code

        Hello.Background = System.Windows.Media.Brushes.Blue;
        var dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.I         


        
4条回答
  •  南笙
    南笙 (楼主)
    2021-01-22 10:57

    Just tried it now ..... however this is code behind

    XAML Code:

       

    Cs File

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

    you can change the color when your event is called and then call your animation.

提交回复
热议问题