列出 WPF 特定窗口的所有控件 [英] List all controls of a specific window of WPF

查看:37
本文介绍了列出 WPF 特定窗口的所有控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 WPF 非常陌生,只是想请教您一种非常基本的获取 Windows 控件及其子项的方法,就像在 Winform 应用程序中一样.底线是在不同的不同类中为多个窗口/页面提供可重用的代码.

I am very new to WPF and just would to ask your help for a very basic method of getting the Windows controls and their children as in Winform app. Bottom line is to have reusable code for multiple window/pages in a different different class.

之前非常感谢.

Public Sub GetControl(Wn As Window)
    For Each Ctrl As Control In Wn.Controls
        'Code here

        If Ctrl.HasChildren = True Then
            'Code here
        End If
    Next
End Sub

推荐答案

所以这里是低点.您需要查找 UIElement,它是 XAML 中所有 UIElement 的基类.主机控件有两种主要类型.内容控制和面板.
ContentControl 具有可能包含对象的内容"属性.面板具有 UIElements 属性Children"和 UIElement 类型的集合.

So here's the down low. You need to look for UIElement, which is a base class for all UIElements in XAML. There are two main types that host controls. ContentControl and Panel.
A ContentControl has a 'Content' property that is potentially containing an object. A Panel has a collection of UIElements property 'Children' and of type UIElement.

如果您只是在寻找 Window 或任何 UIElement 的元素,那么您需要根据该信息递归搜索并制作该列表.

If you're looking just for the elements of a Window or ANY UIElement then you need to recursively search and make that list based on that information.

Window 继承自 ContentControl,但该 Content 可能是 Grid 或 StackPanel 或任何面板或种类,并且也可能具有 UIElement 的子级.

A Window inherits from ContentControl but that Content might be a Grid or StackPanel or any Panel or sorts and may have Children of UIElement also.

您循环浏览它们,直到得到结果.

You cycle through them all until you get the results.

public MainWindow()
{
        InitializeComponent();

        foreach (var element in GetAllElementsFrom(this))
            Debug.WriteLine(element.ToString());
}

private IEnumerable<UIElement> GetAllElementsFrom(UIElement element)
{
    var uiElements = GetSingleElement();

       switch (element)
       {
            case Panel panel:
                foreach (var child in panel.Children)
                    uiElements = uiElements.Concat(GetInnerElements(child));
                break;
            case UserControl userControl:
                uiElements = uiElements.Concat(GetInnerElements(userControl.Content));
                break;
            case ContentControl contentControl:
                if (contentControl.Content is UIElement uiElement)
                    uiElements = uiElements.Concat(GetInnerElements(uiElement));
                break;
        }

    return uiElements;

    IEnumerable<UIElement> GetSingleElement()
    {
        yield return element;
    }
}

这是我使用的 XAML.

Here's the XAML I used.

<Grid>
    <Button>
        <DockPanel>
            <ContentControl>
                <Grid>
                    <TextBox />
                </Grid>
            </ContentControl>
        </DockPanel>
    </Button>

    <StackPanel>
        <Label />
        <TextBlock>
            Hey There!!
        </TextBlock>
        <Grid>
            <Ellipse />
        </Grid>
    </StackPanel>
</Grid>

这是我在调试窗口中得到的结果:

And here's the result I got in my debug window:

System.Windows.Controls.Grid
System.Windows.Controls.Button
System.Windows.Controls.DockPanel
System.Windows.Controls.ContentControl
System.Windows.Controls.Grid
System.Windows.Controls.TextBox
System.Windows.Controls.StackPanel
System.Windows.Controls.Label
System.Windows.Controls.TextBlock
System.Windows.Controls.Grid
System.Windows.Shapes.Ellipse

快乐编码!注意:使用 C# 7 语法;如果您不使用 C# 7,则只需进行更改,我认为它们很简单.

Happy Coding! Note: Uses C# 7 syntax; if you're not on C# 7 then just make the changes, I think they're straight forward.

这篇关于列出 WPF 特定窗口的所有控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆