Muckers Mucker
Muckers Mucker

Reputation: 109

WPF Style not applied to Border element

I'm trying to apply a style so that certain Border elements have an increased border size and colour as follows:

<Border Name="AlarmBorder">
                            <Border.Style>
                                <Style TargetType="{x:Type Border}">
                                    <Setter Property="Background">
                                        <Setter.Value>
                                            <SolidColorBrush Color="{Binding AlarmPriority.BackColour, Converter={StaticResource PriorityBrush}}" />
                                        </Setter.Value>
                                    </Setter>
                                    <Setter Property="BorderBrush">
                                        <Setter.Value>
                                            <SolidColorBrush Color="{Binding AlarmPriority.BackColour, Converter={StaticResource PriorityBrush}}" />
                                        </Setter.Value>
                                    </Setter>
                                     <Style.Triggers>
                                        <DataTrigger Binding="{Binding Path=OPCAlarm.OPCAlarmTriggered}" Value="True">

                                            <Setter Property="BorderThickness" Value="10"/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Path=OPCAlarm.OPCAlarmTriggered}" Value="False">

                                            <Setter Property="BorderThickness" Value="1"/>
                                        </DataTrigger>
                                    </Style.Triggers> 
                                </Style>
                            </Border.Style>

This increases the Border thickness but the colours are not applied. If I move the Colours to within the Border tag as follows:

<Border Name="AlarmBorder" BorderBrush="{Binding AlarmPriority.BackColour, Converter={StaticResource PriorityBrush}}">

I get the borders in the correct colour.

So, the colour applied @the style level does NOT get applied (but the BorderThickness property does), but at the element level the colour comes through.

Can someone tell me what I'm doing wrong?

Thanks

Upvotes: 0

Views: 1056

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81233

Behaviour is due to Dependency property value precedence.

Local set property has higher precedence over style setters so if you set local value, no matter what you set in Style DataTriggers, it won't get applied.

You need to move it to Style setters so that it can be toggled using style DataTriggers.

    <Border Background="Red">
        <Border.Style>
            <Style TargetType="Border">
                <Setter Property="Background" Value="Green"/>
            </Style>
        </Border.Style>
    </Border>

Like in example above, border will always be Red since it has higher precedence order over style setters.

Precedence Order:

  1. Property system coercion.
  2. Active animations, or animations with a Hold behavior.
  3. Local value.
  4. TemplatedParent template properties
  5. Implicit style.
  6. Style triggers.
  7. Template triggers.
  8. Style setters.
  9. Default (theme) style.
  10. Inheritance.
  11. Default value from dependency property metadata.

UPDATE

You are using PriorityBrush Converter, i guess its already converting Color to Brush, so bind directly to background (no need to create another brush). This will work:

<Setter Property="BorderBrush"
        Value="{Binding AlarmPriority.BackColour,
                     Converter={StaticResource PriorityBrush}}"/>

If converter is doing conversion from Color to Brush only, you can omit that completely and do like this:

<Setter.Value>
   <SolidColorBrush Color="{Binding AlarmPriority.BackColour}"/>
</Setter.Value>

Upvotes: 2

Related Questions