Student class
public class Student
{
string name;
List<SubjectInfo> subjectInfoList;
....
}
List<SubjectInfo> List can have different number of SubjectInfo objects for different students.
SubjectInfo struct
public struct SubjectInfo
{
string subjectCode;
int marks;
...
}
I want to display student object detail on a window. Since List have different number of object count I used ItemsControl and used dataTemplate.
Binding a student object works perfectly.
Student student = new Student("Joe", new List<SubjectInfo>() { new SubjectInfo("Subject1", 50), new SubjectInfo("Subject2", 70) });
StudentGrid.DataContext = student;
But this TwoWay binding is not working for SubjectInfo(Changing value from UI will not caught by the codebehind)
But if change SubjectInfo struct to SubjectInfo class it works.
Cannot understand why. Please advice me.
By assigning a
SubjectInfotoDataContext, it is copied (because it is astruct/ValueType), that’s why the binding does not work. This is in most cases not what you want. I suggest you use a class forSubjectInfoinstead.You should be aware of the differences between
classandstruct. The C# Specification (V4.0) summarizes them like this (in section 11.3):System.ValueType(§11.3.2).null(§11.3.4).object(§11.3.5).thisis different for structs (§7.6.7).include variable initializers (§11.3.7).