Dynamically Add Grid UWP

限于喜欢 提交于 2020-05-15 03:38:38

问题


I want to add a grid and its contents on runtime. The Grid is as follows. How can I add this from 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>

回答1:


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);



回答2:


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

Code behind

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);


}


来源:https://stackoverflow.com/questions/36052046/dynamically-add-grid-uwp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!