我有一个关于删除网格的问题:
每当我删除网格中的最后一行或所有行时,网格上仍然会显示一些内容(实际上是最后一行)。我确实检查了 grid.rowdefinitions.count 是否为 0。我使用的方法是 removeAt 和 removeRange(0, grid.rowdefinitions.count)。那有什么问题?谁能告诉我为什么?非常感谢!
//------------
<Grid DockPanel.Dock="Top" HorizontalAlignment="Left" Name="grid1" ShowGridLines="true" Width="200" Height="200">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="100" />
<TextBlock Grid.Row="1" Grid.Column="0" Text="200" />
<TextBlock Grid.Row="2" Grid.Column="0" Text="300" />
<TextBlock Grid.Row="3" Grid.Column="0" Text="400" />
<TextBlock Grid.Row="4" Grid.Column="0" Text="500" />
</Grid>
<StackPanel HorizontalAlignment="Left" Orientation="Horizontal" Width="200" DockPanel.Dock="Top">
<Button Width="80" Click="rem5Row">Remove 5 Rows</Button>
</StackPanel>
//--------------
private void rem5Row(object sender, RoutedEventArgs e)
{
if (grid1.RowDefinitions.Count < 5)
{
Console.WriteLine("less than 5!");
}
else
{
grid1.RowDefinitions.RemoveRange(0,grid1.RowDefinitions.Count);
}
}