为 WPF Datagrid 或 DevexGridcontrol 生成动态列并动态绑定数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14883932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate dynamic columns and bind data dynamically for WPF Datagrid or DevexGridcontrol
提问by WPFKK
I have the following data structure:
我有以下数据结构:
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
What i am interested to achieve in WPF DataGrid/DevExpress GridControl is as follows:
Column 1is always fixed, which is student name and remaining columns will be only based on the number of Scoresand each row should populate the student name and scores.
我有兴趣在 WPF DataGrid/DevExpress GridControl 中实现的内容如下:
Column 1始终是固定的,即学生姓名,其余列将仅基于数量,Scores每行应填充学生姓名和分数。
And each cell should have a two way binding where user can edit the score to reflect back in the actual VM property.
并且每个单元格都应该有一个双向绑定,用户可以在其中编辑分数以反映在实际的 VM 属性中。

I tried to set the AutoGenerateColumnsproperty to true - it generates only two columns because I have only StudentNameand Scoresproperties. So i need some thing that can generate columns from collection for each row.

我试图将该AutoGenerateColumns属性设置为 true - 它只生成两列,因为我只有StudentName和Scores属性。所以我需要一些可以为每一行从集合中生成列的东西。
采纳答案by DmitryG
You can bind grid columns via the GridControl.ColumnsSourceproperty.
Please review this approach detailed description in the following help article: Binding to a Collection of Columns.
您可以通过GridControl.ColumnsSource属性绑定网格列。
请在以下帮助文章中查看此方法的详细说明:绑定到列的集合。

