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?

2 Answers 2

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.

0
43

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

this.txtDescription.SetValue(Grid.RowProperty, 1);
2
  • 1
    I prefer this answer as better answer, because he's used a non-static method. Sep 2, 2018 at 11:15
  • I'd also prefer this answer as the better answer because it's non static.
    – jfmg
    Jan 31, 2019 at 9:30

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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