检查对象列表中的对象类型 [英] Check Object type in List of Objects
本文介绍了检查对象列表中的对象类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为WPF表单添加了一个StackPanel,其中包含带有StackPanels的扩展器.
Got a WPF Form with a StackPanel containing Expanders with StackPanels in it.
<Expander Name="eSoftware" Header="5 Software">
<StackPanel Name="StSoftware" Orientation="Horizontal" HorizontalAlignment="Left" Margin="0,0,8,0">
<StackPanel Margin="0,10" Width="29">
<Image x:Name="img" Height="26" Source="Images/3453120.png" Stretch="Fill" Margin="0,0,0,0"/>
</StackPanel>
<StackPanel Margin="0,10">
<Label x:Name="lbl" Content="Label" Margin="0,0,0,0" />
</StackPanel>
</StackPanel>
</Expander>
我在对象列表中阅读了扩展器的内容. 现在,我必须知道列表是否包含stackpanel类型的对象.
I read the Content of the Expander in a list of Objects. Now i must know if the list contains an object of type stackpanel.
List<Object> tmpList = new List<Object>();
tmpList = ReadChild((StackPanel)exp.Content) //gives out the content of an expander. In the Upper case it is 2 StackPanels
if(tmpList.Contains.typeof(StackPanel)=true) //that's wrong
{
//search for the Stackpanel with lables in it
}
推荐答案
您可以使用OfType<>
过滤列表中的特定类型.我了解您想遍历列表中的所有StackPanel,因此可以执行以下操作:
You can use OfType<>
to filter for just a specific type in a list. I understand that you want to loop through all the StackPanels (in the list), so you can do something like this:
foreach(var panel in tmpList.OfType<StackPanel>()){
//your work here ...
}
如果要检查是否有任何StackPanel,请使用以下方法:
If you want to check if there is any StackPanel, then use this:
if(tmpList.OfType<StackPanel>().Any()){
//...
}
这篇关于检查对象列表中的对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文