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?
+3
A:
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
2009-01-08 19:35:44
Genius! Can the value also be cleared programatically?
Jeremy
2009-01-08 20:17:12
Not sure off the top of my head, you might be able to null it.
Steven Robbins
2009-01-08 20:19:42
+9
A:
To set the value:
textBlock.SetValue(Grid.RowProperty, 3);
To reset the value:
textBlock.SetValue(Grid.RowProperty, null);
Michael S. Scherotter
2009-01-08 22:21:24
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
2009-05-12 13:44:47
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
2009-10-01 19:03:08
+2
A:
Actually to clear a value you should use this:
textBlock.ClearValue(Grid.RowProperty);
Maurice
2009-01-09 18:02:38