如何使用 Linq 查找 UIElement?

发布于 2024-10-11 09:41:37 字数 1669 浏览 7 评论 0原文

我正在尝试这样,但我只得到第一级文本框。如何查询并获取所有文本框

    IEnumerable<UIElement> textboxes =
         from c in this.LayoutRoot.Children
         where c.GetType() == typeof(TextBox)
         select c;

这是我的 xaml:

<UserControl x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Canvas x:Name="LayoutRoot" Background="White">
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="160,112,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <TextBox Height="24" HorizontalAlignment="Left" Margin="76,24,0,0" Name="textBox1" VerticalAlignment="Top" Width="196" Text="test message" />
        <CheckBox Canvas.Left="148" Canvas.Top="64" Content="CheckBox" Height="16" Name="checkBox1" Checked="checkBox1_Checked"/>
        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Horizontal">
                <Grid>
                    <StackPanel Orientation="Vertical">
                        <TextBox Text="test"/>
                        <ComboBox x:Name="combobox" />
                    </StackPanel>
                </Grid>
            </StackPanel>
        </StackPanel>
    </Canvas>
</UserControl>

I am trying like this, but I get only the first level textbox. How do I query and get all the textboxes

    IEnumerable<UIElement> textboxes =
         from c in this.LayoutRoot.Children
         where c.GetType() == typeof(TextBox)
         select c;

Here is my xaml:

<UserControl x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Canvas x:Name="LayoutRoot" Background="White">
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="160,112,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <TextBox Height="24" HorizontalAlignment="Left" Margin="76,24,0,0" Name="textBox1" VerticalAlignment="Top" Width="196" Text="test message" />
        <CheckBox Canvas.Left="148" Canvas.Top="64" Content="CheckBox" Height="16" Name="checkBox1" Checked="checkBox1_Checked"/>
        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Horizontal">
                <Grid>
                    <StackPanel Orientation="Vertical">
                        <TextBox Text="test"/>
                        <ComboBox x:Name="combobox" />
                    </StackPanel>
                </Grid>
            </StackPanel>
        </StackPanel>
    </Canvas>
</UserControl>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南街女流氓 2024-10-18 09:41:37
IEnumarable<DependencyObject> AllDescendent(DependencyObject current)
{
    yield return current;
    int n = VisualTreeHelper.GetChildrenCount();
    for (int k = 0; k < n; k ++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(current, k);
        foreach(var descendent in AllDesendent(child))
        {
            yield return descendent;
        }
    }
}

然后

var textboxes = AllDescendent(layoutRoot).OfType<TextBox>();
IEnumarable<DependencyObject> AllDescendent(DependencyObject current)
{
    yield return current;
    int n = VisualTreeHelper.GetChildrenCount();
    for (int k = 0; k < n; k ++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(current, k);
        foreach(var descendent in AllDesendent(child))
        {
            yield return descendent;
        }
    }
}

Then

var textboxes = AllDescendent(layoutRoot).OfType<TextBox>();
酒中人 2024-10-18 09:41:37

您需要使用 LINQ 中未内置的递归。您可以使用以下扩展方法来实现此目的:

递归 LINQ 查询示例< /a>

You need to use recursion which isn't built into LINQ. Here's an extension method you can use to enable that:

Recursive LINQ Query Example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文