Aaron Glover
Aaron Glover

Reputation: 1249

XAML Style Trigger - Change Style ONLY for Object of with a specific Name

I am new XAML however I am given the task to override some styles for certain elements within an existing application.

In my custom Theme, I am attempting to override the style of a BORDER control.

From what I can tell (using Snoop) to inspect the application, the element I want to change is just a plain border.

The border also seems to have a Name of "SubMenuBorder". Please see the image below.

Snoop Showing Border Name

Here is the latest iteration of my style snippet in which I am trying to set the border control's Background, BorderBrush and BorderThickness BUT ONLY if the control has a name of "SubMenuBorder"

<Style TargetType="{x:Type Border}">
    <Style.Triggers>
        <Trigger Property="Name" Value="SubMenuBorder">
            <Setter Property="Background" Value="Red"></Setter>
            <Setter Property="BorderBrush" Value="Red"></Setter>      
            <Setter Property="BorderThickness" Value="20"></Setter>
        </Trigger>
    </Style.Triggers>
</Style>

Unfortunately the above does NOT work.

The style trigger does not seem to fire/apply to the intended control.

If I simplify things further and just style ALL borders with the following snippet, then it seems to work and the border control I want to change, is styled, but so is every other border control in the application.

<Style TargetType="{x:Type Border}">
    <Setter Property="Background" Value="Red"></Setter>
    <Setter Property="BorderBrush" Value="Red"></Setter>      
    <Setter Property="BorderThickness" Value="20"></Setter>
</Style>

Further Findings

I attempted to use a DataTrigger... which unfortunately doesn't work either.

Snoop shows below that the data trigger is being satisfied, however on the second image below you can see that the property of the background and borderbrush are still from the parenttemplate.

Any ideas please?

<Style TargetType="{x:Type Border}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Name}" Value="SubMenuBorder">
            <Setter Property="Background" Value="Red"></Setter>
            <Setter Property="BorderBrush" Value="Red"></Setter>      
            <Setter Property="BorderThickness" Value="20"></Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>

DataTrigger Satisified

enter image description here

Upvotes: 0

Views: 1439

Answers (1)

mm8
mm8

Reputation: 169200

You cannot use triggers to modify a Border that is defined in a ControlTemplate, with the exception of using an implicit Style that applies to all elements of the type specified by the TargetType property of the implicit Style.

You will either have to modify the ControlTemplate itself, or programmatically find the Border element in the visual tree and then change its runtime property values. The first approach, i.e. modifying or creating a custom template, is the recommended approach.

The name "SubMenuBorder" is only known and applicable within that Border element's namescope.

Upvotes: 1

Related Questions