检查对象列表中的对象类型

阴影

得到了一个WPF表单,其中的StackPanel包含带有StackPanels的扩展器。

<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的对象。

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(在列表中),因此可以执行以下操作:

foreach(var panel in tmpList.OfType<StackPanel>()){
    //your work here ...
}

如果要检查是否有任何StackPanel,请使用以下命令:

if(tmpList.OfType<StackPanel>().Any()){
    //...
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章