如何使用自定义样式和setter和触发器?

时间:2015-02-02 18:34:05

标签: c# wpf button styles

我想做这样的事情,但显然我不能指定2个样式属性。我无法弄清楚将样式资源添加到“<Button.Style>”属性的语法。

<Button x:Name="CreateProductButton" Style="{StaticResource GoldButton}" Content="Add Product" Click="CreateProductButton_Click" TabIndex="4">
        <Button.Style>
            <Style TargetType="{x:Type Button}">
                <Setter Property="IsEnabled" Value="false" />
                <Style.Triggers>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding ElementName=OperatorId, Path=(Validation.HasError)}" Value="false" />
                            <Condition Binding="{Binding ElementName=NewProductId, Path=(Validation.HasError)}" Value="false" />
                            <Condition Binding="{Binding ElementName=NewYearId, Path=(Validation.HasError)}" Value="false" />
                        </MultiDataTrigger.Conditions>
                        <Setter Property="IsEnabled" Value="true" />
                    </MultiDataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

1 个答案:

答案 0 :(得分:1)

如果您尝试使用添加或更改的现有样式,请使用Style.BasedOn。那就是:

<Style x:Key="MyDerivedButtonStyle"
       TargetType="{x:Type Button}"
       BasedOn="{StaticResource GoldButton}">
    <Setter Property="IsEnabled" Value="false" />
    <Style.Triggers>
        ...
    </Style.Triggers>
</Style>

然后将Button的样式设置为Style={StaticResource MyDerivedButtonStyle}