次の 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;
}