XAML 如何在WPF中单击一个按钮来创建多个按钮?

vecaoik1  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(179)

我希望每点击一次“创建按钮”按钮,就会创建一个新的按钮,每列最多8个按钮,然后继续点击“创建按钮”按钮,第二列就会创建新的按钮,以此类推,我想达到的效果是这样的:enter image description here
但目前我是有限的,它是这样的:enter image description here
这里是我的MainWindow.xaml

<Window x:Class="Helloone.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Helloone"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid x:Name="grid" VerticalAlignment="Top" Margin="10">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Button x:Name="button1" Content="Create Button" HorizontalAlignment="Left" VerticalAlignment="Top" Click="Button1_Click" Grid.Row="0" Grid.Column="0"/>
    </Grid>
</Window>

这里是我的MainWindow.xaml.cs

namespace Helloone
{
 /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private int buttonCount = 1;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            // Create a new button
            Button button = new Button();
            button.Content = "Button " + buttonCount.ToString();

            button.HorizontalAlignment = HorizontalAlignment.Left;
            button.VerticalAlignment = VerticalAlignment.Top;
            button.Margin = new Thickness(10);

            // Add the new button to the Grid
            int row = (buttonCount - 1) / 3 + 1;
            int column = (buttonCount - 1) % 3;
            grid.Children.Add(button);
            Grid.SetRow(button, row);
            Grid.SetColumn(button, column);

            // Increment the button count
            buttonCount++;
        }
    }
}
hfsqlsce

hfsqlsce1#

1.为网格添加事件grid_Loaded
1.获取列的编号
1.在每个列/行中创建StackPanel..

public partial class MainWindow : Window
{
   private int numBut = 0;
   private List<StackPanel> stP = new List<StackPanel>();

  public MainWindow()
  {
     InitializeComponent();
  }

 private void Button1_Click(object sender, RoutedEventArgs e)
 {
     if (numBut >= 8 * stP.Count) return;

     var col = numBut / 8;
     var st = stP[col];

     Button button = new Button();
     button.Content = $"Button {++numBut}";
     button.HorizontalAlignment = HorizontalAlignment.Left;
     button.VerticalAlignment = VerticalAlignment.Top;
     button.Margin = new Thickness(10);
     Grid.SetRow(button, 1);
     Grid.SetColumn(button, col);
     st.Children.Add(button);
 }

 private void grid_Loaded(object sender, RoutedEventArgs e)
 {
     var nbCols = grid.ColumnDefinitions.Count;

     for (var c = 0; c < nbCols; c++)
     {
         StackPanel st = new StackPanel();
         st.Orientation = Orientation.Vertical;
         Grid.SetRow(st, 1);
         Grid.SetColumn(st, c);
         grid.Children.Add(st);
         stP.Add(st);
     }
  }
}

结果:

相关问题