5

I have a Grid I am trying to create and populate programatically in Silverlight. However, all of the items just default to spot "0, 0" in the grid despite my efforts. This is what I have:

Grid holdingGrid = new Grid();
int row = 0;

for (int i = 0; i < 10; i++) {
   Expander expander = new Expander();
   holdingGrid.Children.Add(expander);
   Grid.SetRow(expander, row);
   Grid.SetColumn(expander, 0);
   row++;
}

But this still causes all of the items to pile up in the first row and the first column. What am I doing wrong?

2
  • 1
    Have you set row definitions for your grid?
    – ginman
    Jun 29, 2011 at 20:17
  • Ha ha, that would do it. I added the row definitions and it works now. I can't believe I forgot that! Jun 29, 2011 at 20:21

1 Answer 1

6
Grid holdingGrid = new Grid();
int row = 0;

for (int i = 0; i < 10; i++) {
   Expander expander = new Expander();
   holdingGrid.RowDefinitions.Add(new RowDefinition());
   holdingGrid.Children.Add(expander);
   Grid.SetRow(expander, row);
   Grid.SetColumn(expander, 0);
   row++;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.