如何从网格中添加和删除特定列?

How to Add and Remove specific columns from Grid?

当 window ActualWidthActualHeight 超过 700 时,我正在尝试删除特定的 column/row。我真的找不到删除的方法除了

之外的特定 column/row
myGrid.ColumnDefinitions.RemoveAt(1);

每次窗口大小变化时都会删除一列。

当前网格:

<Grid x:Name="myGrid">
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition MaxHeight="80" MinHeight="80"/>
        <RowDefinition MaxHeight="80" MinHeight="80"/>
        <RowDefinition MaxHeight="80" MinHeight="80"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions >
        <ColumnDefinition MaxWidth="170" MinWidth="170" x:Name="ColumnThatNeedsToBeRemovedAndReadded"/>
        <ColumnDefinition MinWidth="110" Width="*"/>
        <ColumnDefinition MinWidth="350" MaxWidth="700"/>
        <ColumnDefinition MinWidth="170" Width="*"/>
    </Grid.ColumnDefinitions>
</Grid>

代码 Adds/Removes:

 if (ActualHeight > 700 && ActualWidth > 700)
 {
    myGrid.ColumnDefinitions.RemoveAt(1);
 }
 else if(ActualHeight < 700 && ActualWidth > 700)
 {
    myGrid.ColumnDefinitions.Add(new ColumnDefinition());
 }

您可以像这样删除特定列:

   for (var index = 0; index < myGrid.ColumnDefinitions.Count; index++)
    {
        var currentCol = myGrid.ColumnDefinitions[index];
        if (currentCol.ActualWidth > 700)
        {
            myGrid.Children.RemoveAt(index);
        }
    }