如何在代码隐藏中对齐控制

本文关键字:对齐 控制 隐藏 代码 | 更新日期: 2023-09-27 18:32:48

如何在代码中对齐控件。

XAML:

<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="60" />
        <RowDefinition />
    </Grid.RowDefinitions>
</<Grid>
<TextBlock HorizontalAlignment="Center" Grid.row="0"/>

在代码隐藏中,我想将DataGrid添加到该网格中。

DataGrid dt = new DataGrid();
// how to add above the grid in to that 
// layoutroot in second row.

如何在代码隐藏中对齐控制

对于第二行,设置 Grid.Row 属性,如下所示:

   DataGrid dt = new DataGrid();
   Grid.SetRow(dt ,1);
   LayoutRoot.Children.Add(dt);
您可以使用 Grid.SetRow()

和 Grid.SetColumn() 函数从代码隐藏设置位置。

Grid.SetRow(dt,1); // set position
LayoutRoot.Children.Add(dt); // add control into the Grid