以xaml代码为例:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="1">
<Label Content="Label"/>
</Grid>
</Grid>
</Grid>
如何在c#代码中创建此表单结构?让我失望的是在父网格的右列中有两列的网格。
感谢您的协助。
答案 0 :(得分:2)
你可以这样做:
public MainWindow()
{
InitializeComponent();
Grid grid1 = new Grid();
grid1.ColumnDefinitions.Add(new ColumnDefinition());
grid1.ColumnDefinitions.Add(new ColumnDefinition());
Content = grid1;
Grid grid2 = new Grid();
grid2.SetValue(Grid.ColumnProperty, 1);
grid2.ColumnDefinitions.Add(new ColumnDefinition());
grid2.ColumnDefinitions.Add(new ColumnDefinition());
grid1.Children.Add(grid2);
Label label = new Label {Content = "Label"};
label.SetValue(Grid.ColumnProperty, 1);
grid2.Children.Add(label);
}