简体   繁体   English

如何通过lambda从网格中删除子UIElement?

[英]How can I remove Child UIElement from a Grid by lambda?

Here is the XAML: 这是XAML:

<Grid x:Name="G">
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <CheckBox Grid.Column="0"></CheckBox>
        <CheckBox Grid.Column="1" IsChecked="True"></CheckBox>
        <CheckBox Grid.Column="2" IsChecked="True"></CheckBox>
        <CheckBox Grid.Column="3"></CheckBox>
    </Grid>

I wanna remove all the CheckBox in the Grid which IsChecked is true. 我想删除IsChecked为true的Grid中的所有CheckBox

So I wrote the code like this: 所以我写了这样的代码:

var RemoveList=G.Children.OfType<CheckBox>().Where(Child => Child.IsChecked == true);
foreach (CheckBox CB in RemoveList.ToList())
      {
      G.Children.Remove(CB);
      }

I think my code is redundant with a var RemoveList and has to change it to the List. 我认为我的代码对于var RemoveList是多余的,必须将其更改为List。

I want to find a way just remove the UIElement only by one line code and makes me learns more about the Lambda. 我想找到一种方法,仅通过一行代码删除UIElement,就可以使我更多地了解Lambda。

Is there any way to do this? 有什么办法吗? Thank you. 谢谢。

G.Children
    .OfType<CheckBox>()
    .Where(child => child.IsChecked == true)
    .ToList()
    .ForEach(child => G.Children.Remove(child));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM