动态添加网格UWP

时间:2016-03-17 05:03:29

标签: c# win-universal-app

我想在运行时添加网格及其内容。网格如下。如何从Code Behind中添加它?

<Grid Width="200" Height="50" Margin="0,50,0,0">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <CheckBox Grid.Column="0" Name="chkBox" MinWidth="32" HorizontalAlignment="Left" Background="Transparent"/>
    <TextBlock Grid.Column="1" Text="Name" FontSize="16" HorizontalAlignment="Left" VerticalAlignment="Center"/>
    <TextBox Grid.Column="2" Name="txtBx" FontSize="16" HorizontalAlignment="Right" VerticalAlignment="Center"/>
</Grid>

3 个答案:

答案 0 :(得分:11)

Grid grid = new Grid();
grid.Width = 200;
grid.Height = 50;
grid.Margin = new Thickness(0, 50, 0, 0);
ColumnDefinition col1 = new ColumnDefinition();
ColumnDefinition col2 = new ColumnDefinition();
ColumnDefinition col3 = new ColumnDefinition();
col1.Width = new GridLength(0, GridUnitType.Auto);
col2.Width = new GridLength(0, GridUnitType.Auto);
col3.Width = new GridLength(1, GridUnitType.Star);
grid.ColumnDefinitions.Add(col1);
grid.ColumnDefinitions.Add(col2);
grid.ColumnDefinitions.Add(col3);
CheckBox cbox = new CheckBox();
cbox.MinWidth = 32;
cbox.HorizontalAlignment = HorizontalAlignment.Left;
cbox.Background = new SolidColorBrush(Windows.UI.Colors.Transparent);
TextBlock tblock = new TextBlock();
tblock.FontSize = 16;
tblock.HorizontalAlignment = HorizontalAlignment.Left;
tblock.VerticalAlignment = VerticalAlignment.Center;
tblock.Text = "text";
TextBox tbox = new TextBox();
tbox.FontSize = 16;
tbox.HorizontalAlignment = HorizontalAlignment.Left;
tbox.VerticalAlignment = VerticalAlignment.Center;
grid.Children.Add(cbox);
grid.Children.Add(tblock);
grid.Children.Add(tbox);
Grid.SetColumn(cbox, 0);
Grid.SetColumn(tblock, 1);
Grid.SetColumn(tbox, 2);

答案 1 :(得分:3)

<StackPanel Margin="0 10 0 0"  Height="100" BorderBrush="Black" BorderThickness="1" Name="signBoardStackPanel">                                              </StackPanel>

背后的代码

private void GenerateGrid()
{
    StackPanel brickStackPanel = new StackPanel();
    brickStackPanel.BorderThickness = new Thickness(1, 1, 1, 1);
    brickStackPanel.BorderBrush = new SolidColorBrush(Colors.Gray);

    for (int bx = 0; bx < 8; bx++)
    {
        StackPanel rowStackPanel = new StackPanel();
        rowStackPanel.Orientation = Orientation.Horizontal;
        for (int by = 0; by < 12; by++)
        {

            Ellipse pixel = new Ellipse();
            pixel.Fill = new SolidColorBrush(Colors.Gray);
            pixel.Height = 4;
            pixel.Width = 4;
            //pixel.Stroke = new SolidColorBrush(Colors.Black);
            rowStackPanel.Children.Add(pixel);

            Rectangle pixel1 = new Rectangle();
            pixel1.Fill = new SolidColorBrush(Colors.White);
            pixel1.Height = 1;
            pixel1.Width = 1;
            rowStackPanel.Children.Add(pixel1);

        }
        brickStackPanel.Children.Add(rowStackPanel);
    }


    signBoardStackPanel.Children.Clear();
    signBoardStackPanel.Children.Add(brickStackPanel);


}

答案 2 :(得分:0)

这是将控件分配给列的另一种方法:

Grid grid = new Grid();
grid.Width = 200;
grid.Height = 50;
grid.Margin = new Thickness(0, 50, 0, 0);
ColumnDefinition col1 = new ColumnDefinition();
ColumnDefinition col2 = new ColumnDefinition();
ColumnDefinition col3 = new ColumnDefinition();
col1.Width = new GridLength(0, GridUnitType.Auto);
col2.Width = new GridLength(0, GridUnitType.Auto);
col3.Width = new GridLength(1, GridUnitType.Star);
grid.ColumnDefinitions.Add(col1);
grid.ColumnDefinitions.Add(col2);
grid.ColumnDefinitions.Add(col3);

CheckBox cbox = new CheckBox();
cbox.MinWidth = 32;
cbox.HorizontalAlignment = HorizontalAlignment.Left;
cbox.Background = new SolidColorBrush(Windows.UI.Colors.Transparent);
TextBlock tblock = new TextBlock();
tblock.FontSize = 16;
tblock.HorizontalAlignment = HorizontalAlignment.Left;
tblock.VerticalAlignment = VerticalAlignment.Center;
tblock.Text = "text";
TextBox tbox = new TextBox();
tbox.FontSize = 16;
tbox.HorizontalAlignment = HorizontalAlignment.Left;
tbox.VerticalAlignment = VerticalAlignment.Center;
grid.Children.Add(cbox);
grid.Children.Add(tblock);
grid.Children.Add(tbox);

//a different way to assign
cbox.SetValue(Grid.ColumnProperty, 0);
tblock.SetValue(Grid.ColumnProperty, 1);
tbox.SetValue(Grid.ColumnProperty, 2);