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

标签 c# wpf styles

我创建了一个覆盖它的 DefaultStyleKey 的自定义窗口,但我丢失了包含在窗口内的所有控件的 FocusVisualStyle。即使是自定义 FocusVisualStyle 也不起作用。我在这里缺少什么?

下面是我如何在 CustomWindow 类的静态构造函数中覆盖 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>

关于c# - WPF - 具有自定义 DefaultStyleKey 的自定义窗口丢失 FocusVisualStyle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6105652/

相关文章:

c# - 写入数据库MVVM C#的首选样式

c# - 用于单元测试的 Global.asax?

c# - 如何使包含 ItemsControl 的 WPF 网格具有相同大小的列

android - Appinvite_styles.xml :5: error When including Google Play Services library to eclipse

c# - 带有子查询和别名的 NHibernate QueryOver

c# - 我应该如何使用手动 DAL 处理复合实体的创建?

WPF 弹出窗口自动关闭

c# - 按需 IEnumerable 绑定(bind)到 ListBox

jquery - 所选元素下方的导航栏线

android - 如何继承style ="?android:listSeparatorTextViewStyle"样式,将线条颜色改为蓝色?