例如,我有一个 wrapper 。
public class Student{
public string Name{get;set}
public int IDNumber{get;set;}
public ObservableCollection<SubjectWrapper> Subjects{get;set;}
}
public class SubjectWrapper{
public string SubjectName{get;set;}
public bool IsSubjectSelected{get;set;}
}
我该如何有效地将其绑定(bind)到Datagrid。可以有任意多个科目。所有学生都将在该科目栏上找到该名称。请看下图。我也想遵循MVVM模式(后面的代码少)。
最佳答案
public partial class MainWindow : Window {
public MainWindow()
{
InitializeComponent();
var student = new Student(){ Name = "Ebin"};
student.Subjects.Add(new SubjectWrapper() { SubjectName="subject1",IsSubjectSelected=true });
student.Subjects.Add(new SubjectWrapper(){ SubjectName = "subject2", IsSubjectSelected = false});
var student2 = new Student() { Name = "Ravi" };
student2.Subjects.Add(new SubjectWrapper() { SubjectName = "subject1", IsSubjectSelected = false });
student2.Subjects.Add(new SubjectWrapper() { SubjectName = "subject2", IsSubjectSelected = true });
var list = new List<Student>();
list.Add(student);
list.Add(student2);
//Name column adding
maingrid.Columns.Add(new DataGridTextColumn(){ Header = "name", Binding = new Binding("Name")});
//Subject columns added dynamically
for (int i = 0; i < list[0].Subjects.Count(); i++) {
var col = new DataGridCheckBoxColumn();
col.Header = list[0].Subjects[i].SubjectName;
col.Binding = new Binding("Subjects[" + i.ToString() + "].IsSubjectSelected");
maingrid.Columns.Add(col);
}
maingrid.ItemsSource = list;
}
}
public class Student
{
public string Name { get; set; }
public int IDNumber { get; set; }
public ObservableCollection<SubjectWrapper> Subjects { get; set; }
public Student()
{
Subjects = new ObservableCollection<WpfApplication1.SubjectWrapper>();
}
}
public class SubjectWrapper {
public string SubjectName { get; set; }
public bool IsSubjectSelected { get; set; }
}
关于c# - 基于集合的动态Datagrid列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39425380/