views:

346

answers:

2

I have a WPF Grid with a XAML similar to this:

  <Grid width=200 Height=200 >

        <Grid.ColumnDefinitions >                             
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="2*" />
         </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"  />
            <RowDefinition Height="Auto"  />
            <RowDefinition Height="Auto" MinHeight="24" />
        </Grid.RowDefinitions>

      <TextBlock Text="Name" Grid.Row="0" Grid.Column="0"/>
      <TextBox Grid.Row="0" Grid.Column="1" />

      <TextBlock Text="Age" Grid.Row="1" Grid.Column="0"/>
      <TextBox Grid.Row="1" Grid.Column="1" />

  </Grid>

I need to add a new row in between existing 2 rows of data, but my worry is that when I add a new row, I will need to manually update Grid.Row attached property in each of the controls that appear in rows below the newly added row.

Is there a smarter way of doing this? may be to set Row/Column numbers relative to adjacent Rows/Columns ?

Cheers.

A: 

You cannot achieve this automatically. You have to manually adjust the value of Grid.Row for every child of the grid, as necessary:

foreach (UIElement child in grid.Children)
            if (Grid.GetRow(child) >= indexOfNewRow)
                Grid.SetRow(Grid.GetRow(child) + 1);
Andy
Thanks Andy.based on your code snippet, wondering whether you assumed that I was adding new rows programatically?I meant to do this on XAML.. and was trying to find whether there's a way to specify Row and Column numbers relative to adjacant Rows/Columns
IG105
A: 

I'm also searching for this answer. The editor for this doesn't appear to be efficient if everytime I add/delete a row I have to go back through XAML and change the grid.row and grid.col for each control.

Has anybody found a easy solution?

CWinKY