WPF-具有自定义DefaultStyleKey的自定义窗口将丢失FocusVisualStyle

问题描述

|| 我已经创建了一个覆盖它的DefaultStyleKey的自定义窗口,但是我丢失了该窗口中包含的所有控件的FocusVisualStyle。甚至自定义FocusVisualStyle都不起作用。我在这里想念什么? 这是我在CustomWindow类的静态ctor中重写DefaultStyleKey的方法
DefaultStyleKeyProperty.OverrideMetadata( typeof( CustomWindow ),new FrameworkPropertyMetadata( typeof( CustomWindow ) ) );
这是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>
下一步是将MainWindow的基本类型更改为CustomWindow并添加两个按钮。使用Tab键进行导航时,没有显示焦点矩形。 任何帮助,将不胜感激!     

解决方法

您需要将
ContentPresenter
放在
AdornerDecorator
内,如下所示:
<AdornerDecorator>
    <ContentPresenter/>
</AdornerDecorator>
是装饰器装饰器渲染所有焦点矩形。 当一切不正常时,您可以查看默认的控件模板。然后,您尝试使用他们的模板和您的模板,并弄清为什么一个可行而另一个却不可行! 我抬起头来
Window
,看起来像这样:
<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>