Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何为XAML动态创建网格布局_C#_Wpf_Xaml_Uwp - Fatal编程技术网

C# 如何为XAML动态创建网格布局

C# 如何为XAML动态创建网格布局,c#,wpf,xaml,uwp,C#,Wpf,Xaml,Uwp,在我的应用程序中,我尝试动态创建网格布局,但未获得预期输出: 下面是我尝试的代码: Grid LayoutGrid = new Grid(); //Created Two Columns ColumnDefinition gridCol1 = new ColumnDefinition(); ColumnDefinition gridCol2 = new ColumnDefinition(); LayoutGri

在我的应用程序中,我尝试动态创建网格布局,但未获得预期输出:

下面是我尝试的代码:

        Grid LayoutGrid = new Grid();


        //Created Two Columns
        ColumnDefinition gridCol1 = new ColumnDefinition();
        ColumnDefinition gridCol2 = new ColumnDefinition();

        LayoutGrid.ColumnDefinitions.Add(gridCol1);
        LayoutGrid.ColumnDefinitions.Add(gridCol2);

        Grid Col1Grid = new Grid();
        //Create Two Rows
        RowDefinition gridRow1 = new RowDefinition();
        RowDefinition gridRow2 = new RowDefinition();

        Col1Grid.RowDefinitions.Add(gridRow1);
        Col1Grid.RowDefinitions.Add(gridRow2);

        Grid.SetColumn(Col1Grid, 1);

        return LayoutGrid;
我尝试创建的布局如下所示:


为了更容易理解,我添加了一些颜色

private Grid CreateGrid()
    {
        var LayoutGrid = new Grid { Background = new SolidColorBrush(Colors.Yellow) };

        //Created Two Columns
        LayoutGrid.ColumnDefinitions.Add(new ColumnDefinition());
        LayoutGrid.ColumnDefinitions.Add(new ColumnDefinition());

        //Created Two Rows
        LayoutGrid.RowDefinitions.Add(new RowDefinition());
        LayoutGrid.RowDefinitions.Add(new RowDefinition());

        // region 1 - vertical left
        var stack1 = new StackPanel {
            Background = new SolidColorBrush(Colors.Red)
        };
        Grid.SetColumn(stack1, 0);
        Grid.SetRowSpan(stack1, 2);
        LayoutGrid.Children.Add(stack1);

        // region 2 - top right
        var stack2 = new StackPanel
        {
            Background = new SolidColorBrush(Colors.Green)
        };
        Grid.SetColumn(stack2, 1);
        LayoutGrid.Children.Add(stack2);

        // region 3 - bottom right
        var stack3 = new StackPanel
        {
            Background = new SolidColorBrush(Colors.Blue)
        };
        Grid.SetColumn(stack3, 1);
        Grid.SetRow(stack3, 1);
        LayoutGrid.Children.Add(stack3);

        return LayoutGrid;
    }
Xaml:



您得到的输出是什么?我只得到两列,第1列中没有创建行。您没有将Col1Grid添加到LayoutGrid您正在创建两个不同的网格,请尝试将您的行定义添加到“Layout Grid”
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <StackPanel Background="Red" Grid.RowSpan="2"></StackPanel>
    <StackPanel Background="Blue" Grid.Column="1"></StackPanel>
    <StackPanel Background="Green" Grid.Column="1" Grid.Row="1"></StackPanel>
</Grid>