Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - Custom Window with custom DefaultStyleKey loses FocusVisualStyle

Tags:

c#

styles

wpf

I've created a custom Window overriding it's DefaultStyleKey but I'm losing the FocusVisualStyle of all the controls that are contained inside of the window. Even a custom FocusVisualStyle is not working. What am I missing here?

Here's how I override the DefaultStyleKey in the static ctor of the CustomWindow class:

DefaultStyleKeyProperty.OverrideMetadata( typeof( CustomWindow ), new FrameworkPropertyMetadata( typeof( CustomWindow ) ) );

Here's the default style defined in the generic.xaml:

<Style TargetType="{x:Type local:CustomWindow}">
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type local:CustomWindow}">
            <Border Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
               <ContentPresenter />
            </Border>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>

Next step is to change the base type of the MainWindow to CustomWindow and add two buttons. While using the Tab key to navigate, no focus rectangle is showing up.

Any help would be appreciated!

like image 915
Jacques Bourque Avatar asked Jan 26 '26 06:01

Jacques Bourque


1 Answers

You need to place your ContentPresenter inside an AdornerDecorator like this:

<AdornerDecorator>
    <ContentPresenter/>
</AdornerDecorator>

It is the adorner decorator that is rendering all the focus rectangles.

When things aren't working you can look at the default control templates. Then you try their template and your template and figure out why one works and the other doesn't!

I looked up Window and it looks like this:

<Style x:Key="{x:Type Window}"
       TargetType="{x:Type Window}">
    <Setter Property="Foreground"
            Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
    <Setter Property="Background"
            Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Window}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <AdornerDecorator>
                        <ContentPresenter/>
                    </AdornerDecorator>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="Window.ResizeMode"
                 Value="CanResizeWithGrip">
            <Setter Property="Template"
                    Value="{StaticResource WindowTemplateKey}"/>
        </Trigger>
    </Style.Triggers>
</Style>
like image 140
Rick Sladkey Avatar answered Jan 28 '26 19:01

Rick Sladkey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!