C# 如何从 wpf 中的代码后面更改控件的 Grid.Row 和 Grid.Column

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8936397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:17:41  来源:igfitidea点击:

How to change the Grid.Row and Grid.Column of the control from code behind in wpf

c#wpf

提问by Kuntady Nithesh

I have the control placed in DataGridlike this:

我把控件放在DataGrid这样的位置:

<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.Rowand Grid.Columnof the control in code behind?

如何更改后面代码中控件的Grid.RowGrid.Column

采纳答案by Random832

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).

还有一个静态方法可以做到这一点(类似于在代码中使用属性来设置非附加属性而不是在那里使用 DP)。

Grid.SetRow(txtDescription, 1);

You may find this more readable.

您可能会发现这更具可读性。

回答by Greg Sansom

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

使用 DependencyObject.SetValue,传入 Grid.Row 的 DependencyProperty 和要分配的值:

this.txtDescription.SetValue(Grid.RowProperty, 1);