C# 对于带有控件模板的窗口,未显示焦点虚线

C# 对于带有控件模板的窗口,未显示焦点虚线,c#,wpf,xaml,C#,Wpf,Xaml,如果我有一个UI来自控件模板的窗口,那么显示键盘焦点的虚线(“焦点视觉”)就不会出现。如果我使用直接内容(而不是控件模板)重新实现,那么焦点视觉效果很好 有人知道在使用控件模板时如何使焦点可视化吗 我最初使用XAML,但为了排除它,我用C#编写了演示代码。我对基于XAML的解决方案也很满意 Class1.cs using System.Windows; using System.Windows.Controls; using System.Windows.Media; class Class

如果我有一个UI来自控件模板的窗口,那么显示键盘焦点的虚线(“焦点视觉”)就不会出现。如果我使用直接内容(而不是控件模板)重新实现,那么焦点视觉效果很好

有人知道在使用控件模板时如何使焦点可视化吗

我最初使用XAML,但为了排除它,我用C#编写了演示代码。我对基于XAML的解决方案也很满意

Class1.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

class Class1 : Window
{
    public Class1()
    {
        Title = "Class1";
        Height = 150;
        Width = 300;

        Template = new ControlTemplate() {
            VisualTree = new FrameworkElementFactory(typeof(Template1))
        };
    }

    class Template1 : StackPanel
    {
        public Template1()
        {
            Background = Brushes.White;
            Children.Add(new TextBox());
            Children.Add(new Button() { Content = "button 1" });
            Children.Add(new Button() { Content = "button 2" });
        }
    }
}
using System.Windows;
using System.Windows.Controls;
class Class2 : Window
{
    public Class2()
    {
        Title = "Class2";
        Height = 150;
        Width = 300;

        Content = new StackPanel
        {
            Children =
            {
                new TextBox(),
                new Button() { Content = "button 1" },
                new Button() { Content = "button 2" },
            }
        };
    }
}
Class2.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

class Class1 : Window
{
    public Class1()
    {
        Title = "Class1";
        Height = 150;
        Width = 300;

        Template = new ControlTemplate() {
            VisualTree = new FrameworkElementFactory(typeof(Template1))
        };
    }

    class Template1 : StackPanel
    {
        public Template1()
        {
            Background = Brushes.White;
            Children.Add(new TextBox());
            Children.Add(new Button() { Content = "button 1" });
            Children.Add(new Button() { Content = "button 2" });
        }
    }
}
using System.Windows;
using System.Windows.Controls;
class Class2 : Window
{
    public Class2()
    {
        Title = "Class2";
        Height = 150;
        Width = 300;

        Content = new StackPanel
        {
            Children =
            {
                new TextBox(),
                new Button() { Content = "button 1" },
                new Button() { Content = "button 2" },
            }
        };
    }
}
MainWindow.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

class Class1 : Window
{
    public Class1()
    {
        Title = "Class1";
        Height = 150;
        Width = 300;

        Template = new ControlTemplate() {
            VisualTree = new FrameworkElementFactory(typeof(Template1))
        };
    }

    class Template1 : StackPanel
    {
        public Template1()
        {
            Background = Brushes.White;
            Children.Add(new TextBox());
            Children.Add(new Button() { Content = "button 1" });
            Children.Add(new Button() { Content = "button 2" });
        }
    }
}
using System.Windows;
using System.Windows.Controls;
class Class2 : Window
{
    public Class2()
    {
        Title = "Class2";
        Height = 150;
        Width = 300;

        Content = new StackPanel
        {
            Children =
            {
                new TextBox(),
                new Button() { Content = "button 1" },
                new Button() { Content = "button 2" },
            }
        };
    }
}
我刚从主窗口启动了自定义窗口

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        (new Class1()).Show();
        (new Class2()).Show();
    }
}

我认为这个问题是因为
Class1
窗口的视觉树中没有装饰层,而
Class2
窗口的视觉树中有装饰层

当框架元素获得键盘焦点时,它似乎试图通过装饰层指定焦点视觉样式。(参考:和的源代码)

选项#1最简单的解决方案是更改代码,在代码中包含一个
ContentControl
(并重新设置模板):

新的视觉树具有装饰层,因此具有焦点视觉样式:

选项#2或者,另一种解决方案是确保您的模板具有
装饰解码器
-

注意:我不太清楚为什么在重新为
内容控件
模板时默认添加装饰器,而在
窗口
的情况下则不添加装饰器。 更新05/25:我们刚刚意识到,在为
ContentControl
使用自定义模板时看到的装饰器来自
Window
的默认模板

选项#3如果使用XAML,只需将元素包装在
中即可:


我认为这个问题是因为
Class1
窗口的视觉树中没有装饰层,而
Class2
窗口的视觉树中有装饰层

当框架元素获得键盘焦点时,它似乎试图通过装饰层指定焦点视觉样式。(参考:和的源代码)

选项#1最简单的解决方案是更改代码,在代码中包含一个
ContentControl
(并重新设置模板):

新的视觉树具有装饰层,因此具有焦点视觉样式:

选项#2或者,另一种解决方案是确保您的模板具有
装饰解码器
-

注意:我不太清楚为什么在重新为
内容控件
模板时默认添加装饰器,而在
窗口
的情况下则不添加装饰器。 更新05/25:我们刚刚意识到,在为
ContentControl
使用自定义模板时看到的装饰器来自
Window
的默认模板

选项#3如果使用XAML,只需将元素包装在
中即可: