46

I have the control placed in DataGrid like this:

<Label Name="lblDescription" HorizontalAlignment="Left" Margin="0,5,0,0" Grid.Row="2" Grid.Column="2" />
<TextBox  Name="txtDescription" HorizontalAlignment="Left" Width="200" Margin="0,5,0,0" TextWrapping="Wrap" VerticalScrollBarVisibility="Visible" AcceptsReturn="True" Grid.RowSpan="2" Grid.Row="2" Grid.Column="3" />

How can I change the Grid.Row and Grid.Column of the control in code behind?

Hamed Rajabi Varamini
  • 3,439
  • 3
  • 24
  • 38
Kuntady Nithesh
  • 11,371
  • 20
  • 63
  • 86

2 Answers2

76

There is also a static method to do this (analogous to using the property in code to set a non-attached property rather than using the DP there).

Grid.SetRow(txtDescription, 1);

You may find this more readable.

Random832
  • 37,415
  • 3
  • 44
  • 63
43

Use DependencyObject.SetValue, passing in the DependencyProperty for Grid.Row and the value you want to assign:

this.txtDescription.SetValue(Grid.RowProperty, 1);
Greg Sansom
  • 20,442
  • 6
  • 58
  • 76