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

c++wpf

I am using this code

        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

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>

Best Solution

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

Example:

<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>
Related Question