C#生成按钮的动态列表

C#生成按钮的动态列表,c#,wpf,dynamic,C#,Wpf,Dynamic,因此,我试图在C#中的WPF中生成按钮的动态列表,但我发现这非常困难。无论我如何尝试,我都会得到一个错误,即“Button”不包含“”的定义(在本例中为文本和停靠,然后我无法转换控件中的按钮。添加(b)行。我真的不明白发生了什么,但我在StackOverflow上找到的解决方案还没有解决这个问题,所以我觉得我应该请人解释一下这里出了什么问题。 感谢所有花时间帮忙的人 using System; using System.Collections.Generic; using System.Wind

因此,我试图在C#中的WPF中生成按钮的动态列表,但我发现这非常困难。无论我如何尝试,我都会得到一个错误,即“Button”不包含“”的定义(在本例中为文本和停靠,然后我无法转换控件中的按钮。添加(b)行。我真的不明白发生了什么,但我在StackOverflow上找到的解决方案还没有解决这个问题,所以我觉得我应该请人解释一下这里出了什么问题。 感谢所有花时间帮忙的人

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;

namespace App_Manager
{
    public partial class GroupListScreen : Window
    {
        SQLiteManager SQLMan;
        public GroupListScreen(SQLiteManager SQLman)
        {
        SQLMan = SQLman;
        GenerateList();
        InitializeComponent();
        }

        private void GenerateList()
        {
            TableLayoutPanel tableLayoutPanel1 = new TableLayoutPanel();
            List<QueryData> listGenerate = SQLMan.getData();
            var rowCount = listGenerate.Count/3;
            var columnCount = 3;

            tableLayoutPanel1.ColumnCount = columnCount;
            tableLayoutPanel1.RowCount = rowCount;

            tableLayoutPanel1.ColumnStyles.Clear();
            tableLayoutPanel1.RowStyles.Clear();

            for (int i = 0; i < columnCount; i++)
            {
            tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100 / columnCount));
            }
            for (int i = 0; i < rowCount; i++)
            {
            tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100 / rowCount));
            }

            for (int i = 0; i < rowCount * columnCount; i++)
            {
                System.Windows.Controls.Button b = new System.Windows.Controls.Button();
                b.Text = (i + 1).ToString();
                b.Name = string.Format("b_{0}", i + 1);
                b.Click += b_Click;
                b.Dock = DockStyle.Fill;
                tableLayoutPanel1.Controls.Add(b);
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Forms;
命名空间应用程序管理器
{
公共部分类GroupListScreen:窗口
{
SQLiteManager-SQLMan;
公共组列表屏幕(SQLItemManager SQLman)
{
SQLMan=SQLMan;
生成论者();
初始化组件();
}
私有无效生成列表()
{
TableLayoutPanel TableLayoutPanel 1=新的TableLayoutPanel();
List listGenerate=SQLMan.getData();
var rowCount=listGenerate.Count/3;
var列数=3;
tableLayoutPanel1.ColumnCount=ColumnCount;
tableLayoutPanel1.RowCount=行计数;
tableLayoutPanel1.ColumnStyles.Clear();
tableLayoutPanel1.RowStyles.Clear();
对于(int i=0;i
在使用WPF时,您应该开始使用MVVM。
生成动态列表是一项非常简单的任务。简短摘要:视图中的ObservableCollection和例如ListView以及ItemsTemplate就是这样。不需要代码隐藏;)

put
InitializeComponent()
mathod before
GenerateList()
WPF不是Winforms。如果要编写Winforms代码,应该使用Winforms。如果你想使用WPF,你需要学习WPF习语。上述任务最好通过使用数据模板来完成,而不是在代码中生成元素。如果必须在代码中生成元素,则需要了解
按钮
具有
内容
属性,而不是
文本
TableLayoutPanel
是Winforms对象,无论如何您都不应该尝试在WPF中使用它。@PeterDuniho感谢您的输入,虽然我在谷歌上搜索了“wpf”,但我对这些东西不是很有经验,所以我没有真正理解这些东西——应该有很多问题与此相关。这是这里被问得最多的问题之一。