Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
906 views
in Technique[技术] by (71.8m points)

wpf - How to make Style.Triggers trigger a different named style to be applied

Lets say I have the below:

<Style TargetType="{x:Type TextBox}">
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="BorderBrush" Value="Gray" />
    <Style.Triggers>
        <Trigger Property="IsFocused" Value="true"> 
            <Setter Property="BorderBrush" Value="Green" />
            <Setter Property="BorderThickness" Value="2" />
        </Trigger>
    </Style.Triggers> 
</Style>

This works fine and there is nothing too much wrong here, but it is a fairly simple case. What happens if I want to have the IsFocused style state listed as a exsplicit style how do reference that style as being the IsFocused style, i.e.

<Style x:key="ActiveStyle" TargetType="{x:Type TextBox}">
    <Setter Property="BorderBrush" Value="Green" />
    <Setter Property="BorderThickness" Value="2" />
</Style>

<Style TargetType="{x:Type TextBox}">
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="BorderBrush" Value="Gray" />
    <Style.Triggers>
        <Trigger Property="IsFocused" Value="true">
           -- Here I want to reference ActiveStyle and not copy the copy the setters
        </Trigger>
    </Style.Triggers> 
</Style>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I don't think you can however, you can reuse a style this way :

<Style x:Key="ActiveStyle" TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <Trigger Property="IsFocused" Value="true">
            <Setter Property="BorderBrush" Value="Green" />
            <Setter Property="BorderThickness" Value="2" />
        </Trigger>
    </Style.Triggers>
</Style>

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource ActiveStyle}">
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="BorderBrush" Value="Gray" />
</Style>

I don't see another solution :(


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...