为WPF Datagrid或DevexGridcontrol动态生成动态列并绑定数据

时间:2013-02-14 20:58:38

标签: wpf mvvm datagrid devexpress

我有以下数据结构:

public class StudentScore {
    public string ScoreValue{ get; set; }
}

public class Student {
    public string StudentName { get; set; }
    //Scores.Count will be = EndDate-StartDate
    public ObservableCollection<StudentScore> Scores { get;set; } 
}

ObservableCollection<Student> Students { get; set; }
public DateTime StartDate { get; set; } //Can be changed by user dynamically
public DateTime EndDate { get; set; } //Can be changed dynamically

我有兴趣在WPF DataGrid / DevExpress GridControl中实现如下: Column 1始终是固定的,即学生姓名,其余列将仅基于Scores的数量,每行应填写学生姓名和分数。

每个单元格都应该有双向绑定,用户可以编辑得分以反映实际的VM属性。

STudent poc image
我尝试将AutoGenerateColumns属性设置为true - 它只生成两列,因为我只有StudentNameScores属性。所以我需要一些可以从每行的集合生成列的东西。

1 个答案:

答案 0 :(得分:1)

您可以通过GridControl.ColumnsSource属性绑定网格列 请在以下帮助文章中查看此方法的详细说明:Binding to a Collection of Columns