C#/WPF:如何以编程方式创建所需数量的文本块以显示来自 table 的数据

C# / WPF : how can I programatically create as many texblocks as needed to show data from a table

我正在尝试用包含来自 table.

的值的文本块填充组合框

我想创建与 select 返回的数据行集中的行一样多的文本块。

然后将这些文本块添加到组合框中。

有人能告诉我这是怎么做到的吗?

这是我的代码:

// instead of doing this I'd rather create them as needed.
TextBlock tbx1 = new TextBlock();
TextBlock tbx2 = new TextBlock();
TextBlock tbx3 = new TextBlock();

// Get all category 1 
DataRow[] cutProblemsRow = gediDataSet.CutProblems.Select("CutProbCategId= " + 1);
        
// If there is any records
if (cutProblemsRow.Length > 0)
{
    // create as many texblock as there are rows here
            
    // cycle between rows
    for (int i = 0; i < cutProblemsRow.Count(); i++)
    {
        // Assign value to textblock
        TextBlock.Text = cutProblemsRow[i]["Problem"].ToString(); 
    }                
}
        
// Add the texblock created to the ComboBox
cmbxProblem.Items.Add(tbx1);
cmbxProblem.Items.Add(tbx2);
cmbxProblem.Items.Add(tbx3);

正如 Clemens 和 zaggler 所建议的那样,最好的方法是:

private void AddProblemCategtoCombobox(int categ)
{
    // clear list
    cmbxProblem.ItemsSource = null;
        
    // get list
    cmbxProblem.ItemsSource = gediDataSet.CutProblems.Select("CutProbCategId= " + categ).Select(dr => dr["Problem"].ToString()).ToList();            
}