我在 XAML 中设置了 ColumnDefinitions
,如下所示:
<Grid x:Name="MainGrid" Background="#FF256BD1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
...
</Grid>
在后面的代码中,我在 MainGrid
中添加了一个 StackPanel
作为子项,例如:
MainGrid.Children.Add(stackPanel);
如何像我们在 XAML 中那样将此 stackPanel 放在代码的第一列中:Grid.Column="0"
或 Grid.ColumnSpan="2"
或 Grid.Row="1"
等
最佳答案
您可以使用 SetRow
、SetColumn
、SetColumnSpan
方法:
Grid.Children.Add(stackPanel);
Grid.SetRow(stackPanel, 1);
Grid.SetColumn(stackPanel, 0);
Grid.SetColumnSpan(stackPanel, 2);
关于c# - 在后面的 C# 代码中设置网格列或网格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38910460/