简体   繁体   English

具有结果数据触发情节提要和鼠标悬停背景的WPF按钮

[英]Wpf button with both result datatrigger storyboard and mouseover background

I'm looking to implement this functionality: A button, bound to a command, that can signal back wether the command was successful in form of a red or green flash. 我正在寻求实现此功能:绑定到命令的按钮,可以以红色或绿色闪烁的形式向后发信号通知命令是否成功。 At the same time, I want the button to look like other buttons. 同时,我希望该按钮看起来像其他按钮。

This is the code I have now: 这是我现在拥有的代码:

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

When the command is executed, ClearedOk is set to "Ok" or "Error" and then to "Empty", this causes the flash I'm looking for, unfortunately, the usual mouseOver-effect is lost for some reason, and if I add a mouseOver-trigger, it always takes precedence over the flashing effect, meaning the flash is only visible of the button isn't mouseOver:ed. 执行命令时,将ClearedOk设置为“ Ok”或“ Error”,然后设置为“ Empty”,这会导致我正在寻找的闪存,不幸的是,由于某些原因,失去了通常的mouseOver-effect,如果我添加mouseOver-trigger,它始终优先于闪烁效果,这意味着仅按钮可见的闪烁不是mouseOver:ed。

Is there a solution to this? 有针对这个的解决方法吗?

Try to make datatemplate and model with properties of state and then binding in template to this properties. 尝试使数据模板和模型具有状态属性,然后将模板绑定到该属性。

 <DataTemplate x:Key="DataTemplate">
        <Grid >
            <Ellipse Stroke="White"
                     StrokeThickness="5"
                     Fill="{Binding State, Converter={StaticResource CheckToColorConverter}}"/>
        </Grid>
    </DataTemplate>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM