【问题标题】:how to wrap dynamic button in stackpanel programmatically? [WPF]如何以编程方式将动态按钮包装在stackpanel中? [WPF]
【发布时间】:2017-08-01 14:31:49
【问题描述】:

我想以编程方式在堆栈面板中以 4 行和 4 列的形式添加 16 个按钮。当我在堆栈面板中添加按钮时,我只能在其中查看 4 个按钮。我无法包装它。我不想使用换行面板来解决问题。(窗口最大化时换行面板会导致问题)。

这是我的代码,

 foreach (var buttonName in list)
 {
   Button newButton = new Button(){Content = "button_name"};
   this.mainPanel.Children.Add(newButton);
 }

XML,

<StackPanel x:Name="mainPanel" Orientation="Horizontal"/>

【问题讨论】:

  • 改用网格。如果您需要横向和纵向管理。你说行和列。网格是您的解决方案
  • 感谢@M.kazemAkhgary 建议将网格作为解决方案。我已经解决了它并发布了我的答案。

标签: c# wpf button stackpanel


【解决方案1】:

这就是你所需要的:

 UniformGrid g = new UniformGrid(){ Rows = 4, Columns = 4};
        g.Children.Add(new Button());
        ... add your 16 buttons ...

UniformGrid 知道如何将其内容呈现为(行 x 列)。

【讨论】:

    【解决方案2】:

    终于,我做到了。感谢 M.kazem Akhgary 建议将网格作为解决方案。

    GridLengthConverter gridLengthConverter = new GridLengthConverter();
                RowDefinition row1 = new RowDefinition();
                row1.Height = (GridLength)gridLengthConverter.ConvertFrom("*");
                RowDefinition row2 = new RowDefinition();
                row2.Height = (GridLength)gridLengthConverter.ConvertFrom("*");
                RowDefinition row3 = new RowDefinition();
                row3.Height = (GridLength)gridLengthConverter.ConvertFrom("*");
                RowDefinition row4 = new RowDefinition();
                row4.Height = (GridLength)gridLengthConverter.ConvertFrom("*");
    
                mainPanel.RowDefinitions.Add(row1);
                mainPanel.RowDefinitions.Add(row2);
                mainPanel.RowDefinitions.Add(row3);
                mainPanel.RowDefinitions.Add(row4);
                ColumnDefinition col1 = new ColumnDefinition();
                col1.Width = (GridLength)gridLengthConverter.ConvertFrom("*");
                ColumnDefinition col2 = new ColumnDefinition();
                col2.Width = (GridLength)gridLengthConverter.ConvertFrom("*");
                ColumnDefinition col3 = new ColumnDefinition();
                col3.Width = (GridLength)gridLengthConverter.ConvertFrom("*");
                ColumnDefinition col4 = new ColumnDefinition();
                col4.Width = (GridLength)gridLengthConverter.ConvertFrom("*");
    
                mainPanel.ColumnDefinitions.Add(col1);
                mainPanel.ColumnDefinitions.Add(col2);
                mainPanel.ColumnDefinitions.Add(col3);
                mainPanel.ColumnDefinitions.Add(col4);
                int row = 0;
                int col = 0;
                foreach (var buttonName in List)
                {
                    if(row<=4)
                    {
                        if (col <= 4)
                        {
                            Button newButton = new Button()
                            {
                                Content = buttonName.Name,
    
                            };
                            Grid.SetRow(newButton, row);
                            Grid.SetColumn(newButton, col);
                            col++;
                            this.mainPanel.Children.Add(newButton);
    
                        }
                        else
                        {
                            row++;
                            col = 0;
                        }
                    }
    
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-18
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多