JohnL4
JohnL4

Reputation: 1156

How to redefine FocusVisualStyle for a WPF user control

I'm trying to redefine FocusVisualStyle for a user control (to make it more obvious in webexes) and it isn't working. Can't I just slap

<Style x:Key="{x:Static SystemParameters.FocusVisualStyleKey}"
       TargetType="Control">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>
                <Rectangle Margin="-3" 
                           StrokeThickness="2"
                           Stroke="Red"
                           SnapsToDevicePixels="true" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style TargetType="Control">
    <Setter Property="FocusVisualStyle"
            Value="{DynamicResource {x:Static SystemParameters.FocusVisualStyleKey}}"/>
</Style>

into my code (in UserControl.Resources)? That doesn't work, though.

It works when I explicitly set the focus visual style on a particular control:

<RadioButton GroupName="rbGroup" TabIndex="30" 
        FocusVisualStyle="{StaticResource {x:Static SystemParameters.FocusVisualStyleKey}}">
    Choice A
</RadioButton>

What have I missed?

Upvotes: 2

Views: 5238

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81253

First of all for style to be picked automatically, TargetType should be same as that of actual control type you intend to apply that on. So, change TargetType of Style from Control to RadioButton.

Second, as per Dependency Property Value Precedence order, properties set in style setters have lesser precedence order compared to properties set in template triggers.

In default template of RadioButton, FocusVisualStyle is set in ControlTemplate triggers. Hence, whatever value you set in Style setters will be overriden by ControlTemplate triggers. And since local value has higher precedence compared to controlTemplate triggers that's why setting it locally works.


There are two possible solutions for it -

First - Either override complete ControlTemplate of RadioButton and place your FocusVisualStyle there.

Second - Style triggers have higher precedence order than control template triggers, set the focusVisualStyle in style triggers and it will be overridden. Something like this:

<Style x:Key="{x:Static SystemParameters.FocusVisualStyleKey}">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>
                <Rectangle Margin="-3" 
                            StrokeThickness="2"
                            Stroke="Red"
                            SnapsToDevicePixels="true"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style TargetType="RadioButton"
       xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Source={x:Static sys:Boolean.TrueString}}"
                        Value="True">
            <Setter Property="FocusVisualStyle"
                    Value="{StaticResource {x:Static SystemParameters.FocusVisualStyleKey}}"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

Upvotes: 3

Related Questions