22

Suppose I have a grid with some row definitions, and a child control in that grid. How would I go about setting the Grid.Row property of the child control programatically?

Jeremy
  • 44,950
  • 68
  • 206
  • 332

3 Answers3

33

To set the value:

textBlock.SetValue(Grid.RowProperty, 3);

To reset the value:

textBlock.SetValue(Grid.RowProperty, null);
Michael S. Scherotter
  • 10,715
  • 3
  • 34
  • 57
  • If you prefer, you could use the idiom: Grid.SetRow(textBlock, 3); Attached properties usually have Get and Set methods (although I don't think it's mandatory so there may be exceptions). – Jim Lynn May 12 '09 at 13:44
  • I notice (this is with the SL3 beta) that to change the location of a child control, you can't just set this property -- you need to remove the control from the parent grid, set the property as shown here, and then re-add it to the parent. – Eric Oct 01 '09 at 19:03
7

Actually to clear a value you should use this:

textBlock.ClearValue(Grid.RowProperty);
Maurice
  • 27,582
  • 5
  • 49
  • 62
4

I'm not 100% sure this is in SilverLight, but in WPF you call a static method (called SetX, where X is the property) on the type the attached property is defined on and pass it in which control to set the value on, and the value:

Grid.SetRow(MyControl, myRowNumber); 
Steven Robbins
  • 26,441
  • 7
  • 76
  • 90