C# 如何将wpf中的所有控件设置为不可聚焦?

C# 如何将wpf中的所有控件设置为不可聚焦?,c#,wpf,xaml,focusable,C#,Wpf,Xaml,Focusable,我有一个wpf应用程序,我想将所有内容设置为Focusable=“false”。 有没有一种简单而优雅的方式?目前,我为我使用的每种类型的控件制作了一种样式,如下所示: <Style TargetType="Button"> <Setter Property="Focusable" Value="False"></Setter> </Style> 有什么更通用的解决方案吗?为什么不试试双线解决方案呢 foreach (var ctrl in

我有一个wpf应用程序,我想将所有内容设置为Focusable=“false”。 有没有一种简单而优雅的方式?目前,我为我使用的每种类型的控件制作了一种样式,如下所示:

<Style TargetType="Button">
<Setter Property="Focusable" Value="False"></Setter>
</Style>


有什么更通用的解决方案吗?

为什么不试试双线解决方案呢

 foreach (var ctrl in myWindow.GetChildren())
{
//Add codes here :)
}  
还要确保添加以下内容:

  public static IEnumerable<Visual> GetChildren(this Visual parent, bool recurse = true)
 {
if (parent != null)
{
    int count = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < count; i++)
    {
        // Retrieve child visual at specified index value.
        var child = VisualTreeHelper.GetChild(parent, i) as Visual;

        if (child != null)
        {
            yield return child;

            if (recurse)
            {
                foreach (var grandChild in child.GetChildren(true))
                {
                    yield return grandChild;
                }
            }
        }
    }
}
}
公共静态IEnumerable GetChildren(此可视父级,bool recurse=true) { 如果(父项!=null) { int count=VisualTreeHelper.GetChildrenCount(父级); for(int i=0;i 甚至更短,请使用以下命令:

public static IList<Control> GetControls(this DependencyObject parent)
{            
    var result = new List<Control>();
    for (int x = 0; x < VisualTreeHelper.GetChildrenCount(parent); x++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(parent, x);
        var instance = child as Control;

        if (null != instance)
            result.Add(instance);

        result.AddRange(child.GetControls());
    } 
    return result;
}
公共静态IList GetControls(此DependencyObject父对象)
{            
var result=新列表();
for(int x=0;x
我发布了一个答案,希望能对您有所帮助。但是,如果您是WPF的初学者,您可以加入myWindow,因为myWindow是window的Name属性,但我找不到getChildren()函数。我缺少什么?你确定吗?…重新检查我的答案:)“System.Windows.Media.Visual”不包含“GetChildren”的定义,并且找不到接受“System.Windows.Media.Visual”类型的第一个参数的扩展方法“GetChildren”(是否缺少using指令或程序集引用?)