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

31,432

Solution 1

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

Solution 2

There is yet a third way to do this.

Create two named control templates for your control:

<ControlTemplate x:Key="NotFocused" TargetType="{x:Type TextBox}">  
    . . .
</ControlTemplate>  

<ControlTemplate x:Key="Focused" TargetType="{x:Type TextBox}">   
    . . .
</ControlTemplate>

Then you create a default style for the TextBox with the triggers in it:

<Style TargetType="{x:Type TextBox}">   
    <Style.Triggers>
        <Trigger Property="IsFocused" Value="True">
            <Setter Property="Template" Value="{StaticResource Focused}" />
        </Trigger>
        <Trigger Property="IsFocused" Value="False">
            <Setter Property="Template" Value="{StaticResource NotFocused}" />
        </Trigger>
    </Style.Triggers>
</Style>

Tony

Solution 3

WPF is providing a special property for this FrameworkElement.FocusVisualStyle So go ahead and assign that :)

<TextBox FocusVisualStyle="{StaticResource ActiveStyle}" .....

Or another way using setters

<Style TargetType="{x:Type TextBox}">
 <Setter Property="BorderThickness" Value="1" />
 <Setter Property="BorderBrush" Value="Gray" />    
 <Setter Property="FocusVisualStyle" >
  <Setter.Value>
    <Style x:key="ActiveStyle" TargetType="{x:Type TextBox}">
       <Setter Property="BorderBrush" Value="Green" />
       <Setter Property="BorderThickness" Value="2" />
    </Style>
   </Setter.Value>
  </Setter>
 </Style>
Share:
31,432
vdhant
Author by

vdhant

Updated on July 09, 2022

Comments

  • vdhant
    vdhant almost 2 years

    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>