简体   繁体   中英

WPF - Custom Window with custom DefaultStyleKey loses FocusVisualStyle

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!

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM