gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:44:23 27 4
gpt4 key购买 nike

我创建了一个覆盖它的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com