如何使用 Linq 查找 UIElement?
我正在尝试这样,但我只得到第一级文本框。如何查询并获取所有文本框
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
然后
Then
您需要使用 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