UWP C#在StackPanel上动态添加按钮并组织

UWP C#在StackPanel上动态添加按钮并组织,c#,xaml,uwp,windows-iot-core-10,C#,Xaml,Uwp,Windows Iot Core 10,我知道有一些帖子询问如何动态添加按钮,但我无法找到如何在stackpanel上组织按钮。 我对添加新按钮没有问题,但有没有办法将它们按列和行组织起来 <Grid Margin="400,0,0,0"> <StackPanel x:Name="stackpanel"> <Button x:Name="Button" Height="30" Width="100" Content="Button" VerticalAlignment="To

我知道有一些帖子询问如何动态添加按钮,但我无法找到如何在
stackpanel
上组织按钮。 我对添加新按钮没有问题,但有没有办法将它们按列和行组织起来

<Grid Margin="400,0,0,0">
     <StackPanel x:Name="stackpanel">
          <Button x:Name="Button" Height="30" Width="100" Content="Button" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="20,20,0,0" Click="Button_Click"/>
     </StackPanel>
</Grid>

private void Button_Click(object sender, RoutedEventArgs e)
    {
        Button b = new Button(); ;
        stackpanel.Children.Add(b);
        b.Content = "Button";
    }

私有无效按钮\u单击(对象发送者,路由目标e)
{
按钮b=新按钮();
stackpanel.Children.添加(b);
b、 Content=“Button”;
}
请帮忙。 谢谢

更新: 我想根据按钮的点击次数添加按钮。它一直添加到第四个rom,然后移动到下一个/新列。

尝试以下操作:

    <Grid Margin="400,0,0,0">
       <UniformGrid x:Name="ButtonsUniformGrid" Columns="2" Rows="4"/>
    </Grid>
然后,每次你点击它都会在网格中放置一个新按钮。如果您对此有任何其他问题,请告诉我:)


如注释所示,如果未在xaml中指定,则Uwp不包括
UniformGrid
。如果您想使用它,只需包括以下内容:

xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
您的
UniformGrid
如下所示:

<controls:UniformGrid...../>

根据我的评论,也可以使用
网格。要实现所需的布局,可以使用以下方法:

    Button b = new Button(); ;
    b.Content = "Button";
    b.Click += Button_Click;
    ButtonsUniformGrid.Children.Add(b);
XAML:

<Grid Margin="400,0,0,0">
        <Grid x:Name="myGrid">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="addBtn" Height="30" Width="100" Content="Button" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="20,20,0,0" Click="Button_Click"></Button>
        </Grid>
    </Grid>
然后我们需要更改
按钮\u单击
方法:

private void Button_Click(object sender, RoutedEventArgs e)
        {

            //Create the button
            Button b = new Button();
            b.Height = 30;
            b.Width = 100;
            b.VerticalAlignment = VerticalAlignment.Top;
            b.HorizontalAlignment = HorizontalAlignment.Left;
            b.Margin = new Thickness(20, 20, 0, 0);
            b.Content = "Button " + buttonCounter;
            b.Click += Button_Click;

            //Calculate the place of the button
            int column = (int)(buttonCounter / 4);
            int row = buttonCounter % 4;

            //Check if you need to add a columns
            if(row == 0)
            {
                ColumnDefinition col = new ColumnDefinition();
                col.Width = new GridLength(column, GridUnitType.Auto);
                myGrid.ColumnDefinitions.Add(col);
            }

            //Add the button
            myGrid.Children.Add(b);
            Grid.SetColumn(b, column);
            Grid.SetRow(b, row);
            buttonCounter++;
        }

在这种方法中,新按钮的位置会自动计算,如果需要,会在网格中添加一个新列。

您可以指定“按列和行组织它们”的含义吗?我想您要求的是
项控件
@TheTanic我已经添加了预期按钮布局的图示。你能给我个建议吗。谢谢。@mylim Grid是您的解决方案,为什么不使用它呢?@mylim看一下我的答案,看看您的问题的网格解决方案UniforMgrId不包括在香草UWP中。你应该添加一个信息,这是来自,如果我没有错,你可以使用它。如果我的答案解决了op问题,它比另一个更简单,仍然不正确。首先,您需要安装nugget packageI提供的工具包。我已经测试了您的代码,但第一个按钮位于第二行<代码>网格。设置行(b,行)从第1行开始。我已更新了
按钮计数器=0
,并删除了默认的
列定义@谢谢@MichaelXu MSFT。。。在探索代码之后。。这给我带来了另一个问题,我如何控制这些动态创建的按钮事件?例如,按钮1打开LED 1,按钮2打开LED 2等,以移除按钮
myGrid.Children.remove(b)如何指定要删除的相应按钮以及删除后如何重新对齐行和列,以使它们之间没有空格。谢谢。@MichaelXu MSFT谢谢你的建议。但它需要从第1行开始,因为我们在xaml中有一个按钮的首字母。我们还需要默认的ColumnDefinition,因为当我们删除它时,在codebehind中创建的第一个列将作为第一个而不是第二个来处理。@mylim请创建新问题以解决新问题。我很高兴能帮助你解决这个问题
private void Button_Click(object sender, RoutedEventArgs e)
        {

            //Create the button
            Button b = new Button();
            b.Height = 30;
            b.Width = 100;
            b.VerticalAlignment = VerticalAlignment.Top;
            b.HorizontalAlignment = HorizontalAlignment.Left;
            b.Margin = new Thickness(20, 20, 0, 0);
            b.Content = "Button " + buttonCounter;
            b.Click += Button_Click;

            //Calculate the place of the button
            int column = (int)(buttonCounter / 4);
            int row = buttonCounter % 4;

            //Check if you need to add a columns
            if(row == 0)
            {
                ColumnDefinition col = new ColumnDefinition();
                col.Width = new GridLength(column, GridUnitType.Auto);
                myGrid.ColumnDefinitions.Add(col);
            }

            //Add the button
            myGrid.Children.Add(b);
            Grid.SetColumn(b, column);
            Grid.SetRow(b, row);
            buttonCounter++;
        }