views:

2017

answers:

3

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
Genius! Can the value also be cleared programatically?
Jeremy
Not sure off the top of my head, you might be able to null it.
Steven Robbins
+9  A: 

To set the value:

textBlock.SetValue(Grid.RowProperty, 3);

To reset the value:

textBlock.SetValue(Grid.RowProperty, null);
Michael S. Scherotter
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
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
+2  A: 

Actually to clear a value you should use this:

textBlock.ClearValue(Grid.RowProperty);
Maurice