user1189507
user1189507

Reputation: 21

Srange result about wpf grid row removal

I have a question about grid removal:

whenever I delete the last row or all rows in the grid, there is still something(actually the last row) displayed on the grid. I did actually checked the grid.rowdefinitions.count is 0. the method I used is removeAt and removeRange(0, grid.rowdefinitions.count). what's wrong with that? Can anybody tell why? Many thanks!

//------------
<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);
            }
        }

Upvotes: 0

Views: 266

Answers (1)

Dan Puzey
Dan Puzey

Reputation: 34198

I'm not sure I understand your question correctly (and I'm not sure the code reflects what you say you're doing). But: how do you know the "final row" is displaying?

What I suspect is happening is this: you are removing rows and expecting the controls in those rows to be removed also. However, this is not how a WPF Grid functions: the controls are not the child of their row, and they are not being removed when you remove the RowDefinition.

So, all of the child controls will still be rendering but, because their Grid.Row is invalid, they will render in the "Row 0" position. They are rendered in markup-order, so the final result will appear as though the "last row" is being displayed because this is the control that's drawn last. In fact, all of your controls are still present; they're just all in the same place on screen.

If you want to confirm this, you could use Snoop to examine your runtime UI. Alternatively, set widths for your textboxes so that the row 0 box is wider - you will see the overlap clearly then.

Upvotes: 1

Related Questions