Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Element to Grid and set its Row Windows Phone

Tags:

xaml

Lets say you want to add a new element to a Grid with the following code:

ExpanderView newExpander = new ExpanderView();
newExpander.Name = name;
newExpander.Header = header;
newExpander.FontSize = 40;

if (row == 0)
    newExpander.IsExpanded = true;

foreach (Item li in items)
{
     CheckBox tb = new CheckBox();
     tb.Content = content;
     tb.FontSize = 20;

     newExpander.Items.Add(tb);
}


MyGrid.Children.Add(newExpander);
Grid.SetRow(newExpander, row);

And now the grid in xaml:

<Grid x:Name="MyGrid">
                        <Grid.RowDefinitions>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                        </Grid.RowDefinitions>
                    </Grid>

Now the last line Grid.SetRow(newExpander, row) in the cs code portion,
How can you tell it which grid you want to set the new row for if you have multiple grids in the .xaml code?

like image 796
iCobot Avatar asked Nov 18 '25 20:11

iCobot


1 Answers

The following code

Grid.SetRow(newExpander, row);

only sets Grid.Row="1" if row is 1 in your object properties.

To use this properties the grid that contains the object must have it's RowDefinitions set

How can you tell it which grid you want to set the new row for if you have multiple grids in the .xaml code?

You don't need to tell since the object remains in the Grid in witch you added it.
When you change the Property Grid.Row it only sets on witch row of the grid, your control will be rendered

like image 112
csharpwinphonexaml Avatar answered Nov 21 '25 08:11

csharpwinphonexaml



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!