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

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

I am using this code

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


        
4条回答
  •  Happy的楠姐
    2021-01-22 11:03

    Just to show a different approach really ... If you're using MVVM, you could bind your button color to a property on the ViewModel, and once you click on it, run your 2 minutes background/timer . Once the 2 minutes are done, it'll change the color to the other one.

    Not much xaml involved, and I do like some of the other solutions here :)

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

    (Assuming you have the ButtonColorPropertyName or whatever you want to name it, in the ViewModel)

提交回复
热议问题