C# 更改wpf C中按钮的颜色#单击后,2分钟后保留原始颜色
我正在使用这个代码C# 更改wpf C中按钮的颜色#单击后,2分钟后保留原始颜色,c#,wpf,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); dispatcherT
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#
Xaml
您是否可以尝试使用在Blend中创建的简单故事板,然后应用于按钮/样式 大概是这样的:
<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>
刚刚试过。。。。。然而,这是代码隐藏 XAML代码:
<Button Content="Button" x:Name="MyButton" Height="23" HorizontalAlignment="Left"
Margin="94,128,0,0" VerticalAlignment="Top" Width="75"/>
您可以在调用事件时更改颜色,然后调用动画。您只需创建
样式
,并使用触发器
以彩色动画
启动情节提要
例如:
<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>
只是为了展示一种不同的方法。。。如果您使用的是MVVM,则可以将按钮颜色绑定到ViewModel上的属性,单击该属性后,运行2分钟的背景/计时器。这两分钟结束后,它会将颜色更改为另一种颜色 不涉及太多xaml,我确实喜欢这里的其他一些解决方案:)
(假设您在ViewModel中有
按钮ColorPropertyName
或您想命名的任何名称)我们这里缺少一些代码。这是从计时器触发的方法的一部分吗?展示整个方法。你说的“淡入淡出”是什么意思?你在用什么?MVVM?代码隐藏?你的按钮是如何定义的?伙计们,我已经添加了我的C#和xaml代码。淡入淡出我的意思是,在勾选之后,红色变暗了,然后又回来了。。。喜欢使用动画的解决方案,但这将只是慢慢地从一种颜色转到另一种颜色,然后再返回。。。我以为用户在点击时想要一种颜色,2分钟后,砰,不同的颜色。。。(我想,因为他不是很具体:)是的,在发布答案后我想到了:)…OP可以修改他想要的方式。关键是在第二个动画开始时,在你的情况下,这将是2分钟。你不需要将行“Prop=Colors.Red”发送到Gui线程吗?不。。。这就是它的美。。。视图模型甚至不知道视图。不管怎么说,他知道点击两秒钟后,他会改变颜色。视图绑定到该属性,这就是魔法的工作原理:)
<Button Content="Button" x:Name="MyButton" Height="23" HorizontalAlignment="Left"
Margin="94,128,0,0" VerticalAlignment="Top" Width="75"/>
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;
}
<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>
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
Thread.Sleep(2000); // two second
ButtonColorPropertyName= Colors.Red;
}