WPF多重触发器&按钮样式上的DataTrigger

时间:2018-01-17 14:01:12

标签: wpf button triggers styles datatrigger

使用"数据输入样式"程序。在某个数据组合更改后,属性IsSaveNeeded(枚举:是,否)设置为“是”,并且“保存”按钮被假设为闪烁。

我已经能够通过基本按钮使按钮闪烁

<!--Save Button-->
    <Button Content="   Save&#13;Settings&#13;  to File" x:Name="SaveBtn" Command="{Binding SaveCMD}" Padding="10,5" Margin= "10,0" IsEnabled="{Binding SaveEnabled}" 
            Background="{StaticResource {x:Static SystemColors.ControlLightBrushKey}}">
        <Button.Style>
            <Style TargetType="Button">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsSaveNeeded}" Value="Yes">
                        <DataTrigger.EnterActions>
                            <BeginStoryboard x:Name="FlashBackground">
                                <Storyboard BeginTime="00:00:00" RepeatBehavior="Forever" >
                                    <ColorAnimation  Storyboard.TargetProperty="Background.Color" Duration="00:00:00.75" AutoReverse="True" To="Red" />
                                </Storyboard>
                            </BeginStoryboard>
                        </DataTrigger.EnterActions>

                        <DataTrigger.ExitActions>
                            <StopStoryboard BeginStoryboardName="FlashBackground" />
                        </DataTrigger.ExitActions>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

我被要求将所有按钮设为自定义样式按钮。

 <!--My Custom Buton-->
<Style x:Key="myBtn" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="White" />
    <Setter Property="Margin" Value="0" />
    <Setter Property="FontWeight" Value="Bold" />
    <Setter Property="Foreground" Value="{StaticResource myColorBrush}" />

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">

                <Grid UseLayoutRounding="True" SnapsToDevicePixels="True">
                    <!--Button Content / DropShadow / Border-->
                    <Border Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}" CornerRadius="5" BorderThickness="1" BorderBrush="{StaticResource myColorBrush}">
                        <Border.Effect>
                            <DropShadowEffect ShadowDepth="4" Direction="330" Color="Black" Opacity="0.2" BlurRadius="4"/>
                        </Border.Effect>
                    </Border>

                    <Border Padding="{TemplateBinding Padding}" UseLayoutRounding="True" SnapsToDevicePixels="True">
                        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="{TemplateBinding Content}" SnapsToDevicePixels="True" >
                            <!--<TextBlock.Effect>
                                <DropShadowEffect ShadowDepth="4" Direction="330" Color="{StaticResource MyColor}" Opacity="0.3" BlurRadius="4"/>
                            </TextBlock.Effect>-->
                        </TextBlock>
                    </Border>
                </Grid>

            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" Value="#FFC1C1C1" />
            <Setter Property="Foreground" Value="#FFACA8A8" />
        </Trigger>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="{StaticResource myHoverBrush}"/>
        </Trigger>
        <Trigger Property="IsPressed" Value="True">
            <Setter Property="Background" Value="{StaticResource myColorBrush}"/>
            <Setter Property="Foreground" Value="White"/>
        </Trigger>
    </Style.Triggers>
</Style>

我需要将IsSaveNeeded DataTrigger添加到此自定义按钮样式myFlashingBtn,但是当我执行Flashing工作但其他触发器停止工作时。

如何将IsSaveNeeded Flashing合并到我的自定义按钮样式中并使所有触发器保持工作?除非我写错了,否则MultiTrigger和MultiDataTriggers没有帮助。

1 个答案:

答案 0 :(得分:0)

我相信您正在寻找BasedOn属性(Style.BasedOn)。 这允许您继承特定样式的所有内容并添加到其中。

<!--Save Button-->
<Button Content="   Save&#13;Settings&#13;  to File" x:Name="SaveBtn" Command="{Binding SaveCMD}" Padding="10,5" Margin= "10,0" IsEnabled="{Binding SaveEnabled}" >
    <Button.Style>
        <Style TargetType="Button" BasedOn="{StaticResource myBtn}">
            <Setter Property="Background" Value="{StaticResource {x:Static SystemColors.ControlLightBrushKey}}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsSaveNeeded}" Value="Yes">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard x:Name="FlashBackground">
                            <Storyboard BeginTime="00:00:00" RepeatBehavior="Forever" >
                                <ColorAnimation  Storyboard.TargetProperty="Background.Color" Duration="00:00:00.75" AutoReverse="True" To="Red" />
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>

                    <DataTrigger.ExitActions>
                        <StopStoryboard BeginStoryboardName="FlashBackground" />
                    </DataTrigger.ExitActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

修改:我将Background移动到了样式的设置者