我想将以下 XAML 示例代码转换为其 C# 等效代码:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<!-- Sub-grid on left -->
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Content="Left, Row 0" Background="Azure" Grid.Row="0"/>
<Label Content="Left, Row 2" Background="Lavender" Grid.Row="2"/>
<GridSplitter Grid.Row="1" Height="8" Background="DarkSlateBlue"
HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
</Grid>
<!-- Sub-grid on right -->
<Grid Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Content="Right, Row 0" Background="Moccasin" Grid.Row="0"/>
<Label Content="Right, Row 2" Background="Honeydew" Grid.Row="2"/>
<GridSplitter Grid.Row="1" Height="8" Background="DarkSlateBlue"
HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
</Grid>
<!-- Splitter between left/right sub-grids -->
<GridSplitter Grid.Column ="1" Width="8" Background="DarkSlateBlue"
VerticalAlignment="Stretch" HorizontalAlignment="Center"/>
到目前为止,这是我的代码:
Grid DynamicGrid = new Grid();
ColumnDefinition gridColDef0 = new ColumnDefinition();
ColumnDefinition gridColDef1 = new ColumnDefinition();
gridColDef1.Width = new GridLength(1, GridUnitType.Auto);
ColumnDefinition gridColDef2 = new ColumnDefinition();
DynamicGrid.ColumnDefinitions.Add(gridColDef0);
DynamicGrid.ColumnDefinitions.Add(gridColDef1);
DynamicGrid.ColumnDefinitions.Add(gridColDef2);
RowDefinition gridRowDef0 = new RowDefinition();
RowDefinition gridRowDef1 = new RowDefinition();
gridRowDef1.Height = new GridLength(1, GridUnitType.Auto);
RowDefinition gridRowDef2 = new RowDefinition();
但我不知道如何将网格行定义添加到第 0 列。我预计会有一些类似于 DynamicGrid.Columns[0].RowDefinitions.Add 的内容,但我还没有找到这样的东西。你能帮助我吗?谢谢!
编辑:为了将来参考,这里是等效的 C# 代码。请注意,它可以优化!
private void CreateDynamicWPFGrid()
{
// Create the Grid
Grid dynamicGrid = new Grid();
ColumnDefinition gridColDef0 = new ColumnDefinition();
ColumnDefinition gridColDef1 = new ColumnDefinition();
gridColDef1.Width = new GridLength(1, GridUnitType.Auto);
ColumnDefinition gridColDef2 = new ColumnDefinition();
dynamicGrid.ColumnDefinitions.Add(gridColDef0);
dynamicGrid.ColumnDefinitions.Add(gridColDef1);
dynamicGrid.ColumnDefinitions.Add(gridColDef2);
// Sub-grid on left.
{
Grid leftSubGrid = new Grid();
RowDefinition gridRowDef0 = new RowDefinition();
RowDefinition gridRowDef1 = new RowDefinition();
gridRowDef1.Height = new GridLength(1, GridUnitType.Auto);
RowDefinition gridRowDef2 = new RowDefinition();
leftSubGrid.RowDefinitions.Add(gridRowDef0);
leftSubGrid.RowDefinitions.Add(gridRowDef1);
leftSubGrid.RowDefinitions.Add(gridRowDef2);
Label label;
label = new Label();
label.Content = "Left, Row 0";
label.Background = new SolidColorBrush(Colors.Azure);
Grid.SetRow(label, 0);
leftSubGrid.Children.Add(label);
label = new Label();
label.Content = "Left, Row 2";
label.Background = new SolidColorBrush(Colors.Lavender);
Grid.SetRow(label, 2);
leftSubGrid.Children.Add(label);
GridSplitter gridSplitter = new GridSplitter();
gridSplitter.Height = 8;
gridSplitter.Background = new SolidColorBrush(Colors.DarkSlateBlue);
gridSplitter.HorizontalAlignment = HorizontalAlignment.Stretch;
gridSplitter.VerticalAlignment = VerticalAlignment.Center;
Grid.SetRow(gridSplitter, 1);
leftSubGrid.Children.Add(gridSplitter);
Grid.SetColumn(leftSubGrid, 0);
dynamicGrid.Children.Add(leftSubGrid);
}
// Sub-grid on right.
{
Grid rightSubGrid = new Grid();
RowDefinition gridRowDef0 = new RowDefinition();
RowDefinition gridRowDef1 = new RowDefinition();
gridRowDef1.Height = new GridLength(1, GridUnitType.Auto);
RowDefinition gridRowDef2 = new RowDefinition();
rightSubGrid.RowDefinitions.Add(gridRowDef0);
rightSubGrid.RowDefinitions.Add(gridRowDef1);
rightSubGrid.RowDefinitions.Add(gridRowDef2);
Label label;
label = new Label();
label.Content = "Left, Row 0";
label.Background = new SolidColorBrush(Colors.Moccasin);
Grid.SetRow(label, 0);
rightSubGrid.Children.Add(label);
label = new Label();
label.Content = "Left, Row 2";
label.Background = new SolidColorBrush(Colors.Honeydew);
Grid.SetRow(label, 2);
rightSubGrid.Children.Add(label);
GridSplitter gridSplitter = new GridSplitter();
gridSplitter.Height = 8;
gridSplitter.Background = new SolidColorBrush(Colors.DarkSlateBlue);
gridSplitter.HorizontalAlignment = HorizontalAlignment.Stretch;
gridSplitter.VerticalAlignment = VerticalAlignment.Center;
Grid.SetRow(gridSplitter, 1);
rightSubGrid.Children.Add(gridSplitter);
Grid.SetColumn(rightSubGrid, 2);
dynamicGrid.Children.Add(rightSubGrid);
}
{
GridSplitter gridSplitter = new GridSplitter();
gridSplitter.Width = 8;
gridSplitter.Background = new SolidColorBrush(Colors.DarkSlateBlue);
gridSplitter.HorizontalAlignment = HorizontalAlignment.Center;
gridSplitter.VerticalAlignment = VerticalAlignment.Stretch;
Grid.SetColumn(gridSplitter, 1);
dynamicGrid.Children.Add(gridSplitter);
}
// Display grid into a Window
Content = dynamicGrid;
}