简体   繁体   中英

How can i use a custom style with setters and triggers?

I would like to do something like this, but obviously I cannot have 2 style properties specified. I cant figure out the syntax for adding the style resource to the " <Button.Style> " attribute.

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

If you're trying to use an existing style with a few additions or changes, used Style.BasedOn . That is:

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

Then set your Button's style as Style={StaticResource MyDerivedButtonStyle} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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