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

后端 未结 4 1907
眼角桃花
眼角桃花 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:

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

    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.

    0 讨论(0)
  • 2021-01-22 10:58

    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>
    
    0 讨论(0)
  • 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)

    0 讨论(0)
  • 2021-01-22 11:07

    could you try to use a simple StoryBoard created in Blend and then apply to Button/style something like this:

    <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>
    
    0 讨论(0)
提交回复
热议问题