Search code examples
c#.netsilverlightgridsilverlight-5.0

c# : How ot create a grid row array programatically


I am working in c# and i am under a situation where i have a grid (childGrid) and inside this grid i want to create 3 more grids dynamically.

I want to achieve it using arrays. My try to do this is:

Grid[] row = new Grid[counts]; //counts=3 in my case but decde dynamically.
for (int i = 0; i < counts; i++)
{
    row[i].RowDefinitions.Add(new RowDefinition());
}

I do so because i have one childGrid (parent grid) on that i will have 3 containers (3 more grid as container as child of chidGrid) so it is row[i] in my case (for i :0 to <3) (if you see the code). and on row[0]. i have a checkbox and two different UIelements at row[1 & 2] . I take different containers because if i select the check of row[0](checkbox) it will set the opacity of row[1].opacity=0.5; and on unchecking it will do row[2].opacity=0.5;. So thats why i have different 3 grids container on a grid.

the line row[i].RowDefinitions.Add(new RowDefinition()); gives warning.

The object reference is not set to an instance of an object.

How to achieve this ? I can not do statically because i dont know statically the value of counts(which i assumed 3 here)


Solution

  • You didn't actually create any grids before you tried to add rows to them.

    Grid[] row = new Grid[counts];
    for (int i = 0; i < counts; i++)
    {
        row[i] = new Grid();
        row[i].RowDefinitions.Add(new RowDefinition());
    }