C# Wpf按钮,具有结果数据触发器情节提要和鼠标悬停背景
我希望实现这个功能:一个按钮,绑定到一个命令上,它可以以红色或绿色闪烁的形式发出命令是否成功的信号。同时,我希望按钮看起来像其他按钮 这是我现在拥有的代码:C# Wpf按钮,具有结果数据触发器情节提要和鼠标悬停背景,c#,wpf,datatrigger,C#,Wpf,Datatrigger,我希望实现这个功能:一个按钮,绑定到一个命令上,它可以以红色或绿色闪烁的形式发出命令是否成功的信号。同时,我希望按钮看起来像其他按钮 这是我现在拥有的代码: <Button IsEnabled="{Binding EmptyingAllowed}" Content="Töm lista" Foreground="#555555" FontWeight="SemiBold" Height="20" Width="65" Command="{Binding EmptyListCommand}
<Button IsEnabled="{Binding EmptyingAllowed}" Content="Töm lista" Foreground="#555555" FontWeight="SemiBold" Height="20" Width="65" Command="{Binding EmptyListCommand}" >
<Button.Style>
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border BorderBrush="#BBBBBB" Padding="1" BorderThickness="0,0,1,1" Background="#DDDDDD">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding ClearedOk}" Value="Ok">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation AccelerationRatio="0.2" DecelerationRatio="0.01" AutoReverse="True" Duration="0:0:1" To="LawnGreen" Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
<DataTrigger Binding="{Binding ClearedOk}" Value="Error">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation AccelerationRatio="0.2" DecelerationRatio="0.01" AutoReverse="True" Duration="0:0:1" To="Red" Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
执行命令时,ClearedOk设置为“Ok”或“Error”,然后设置为“Empty”,这会导致我正在寻找的闪光,不幸的是,由于某种原因,通常的鼠标悬停效果会丢失,如果我添加鼠标悬停触发器,它总是优先于闪光效果,这意味着闪光灯仅在按钮未被鼠标覆盖时可见:ed
有解决方案吗?尝试使用状态属性创建datatemplate和model,然后在模板中绑定到此属性
<DataTemplate x:Key="DataTemplate">
<Grid >
<Ellipse Stroke="White"
StrokeThickness="5"
Fill="{Binding State, Converter={StaticResource CheckToColorConverter}}"/>
</Grid>
</DataTemplate>
此代码现在有效吗?因为我看不到任何闪光stuff@MårtenThurén您是否尝试针对彩色动画设置FillBehavior=“Stop”
?Dhaval,是的,您是否正确设置了ClearedOk设置?按照我的方式,我将它设置为Ok,然后直接清空。dkozl,这实际上起了作用!我得到的效果实际上是,我的闪烁颜色与鼠标上方的背景颜色混合,直到闪烁颜色“达到峰值”,因此,在中途,如果我的颜色是背景:黄色和闪烁:红色,我会得到橙色。幸运的是,我的鼠标盖颜色是淡蓝色,我的闪烁颜色是红色和绿色,所以看起来很好,谢谢!很抱歉,我不太了解这一点,无法了解它如何适合我的解决方案。