【问题标题】:WPF - Custom Window with custom DefaultStyleKey loses FocusVisualStyleWPF - 具有自定义 DefaultStyleKey 的自定义窗口丢失 FocusVisualStyle
【发布时间】:2011-05-24 13:17:11
【问题描述】:

我创建了一个自定义窗口,覆盖了它的 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 键导航时,没有显示焦点矩形。

任何帮助将不胜感激!

【问题讨论】:

    标签: c# wpf styles


    【解决方案1】:

    您需要将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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-16
      • 1970-01-01
      相关资源
      最近更新 更多