学生班
public class Student
{
string name;
List<SubjectInfo> subjectInfoList;
....
}
Run Code Online (Sandbox Code Playgroud)
List<SubjectInfo>SubjectInfo对于不同的学生, 列表可以有不同数量的对象。
主题信息结构体
public struct SubjectInfo
{
string subjectCode;
int marks;
...
}
Run Code Online (Sandbox Code Playgroud)
我想在窗口上显示学生对象的详细信息。由于列表有不同数量的对象计数,我使用ItemsControl并使用了 dataTemplate。
绑定学生对象效果非常好。
Student student = new Student("Joe", new List<SubjectInfo>() { new SubjectInfo("Subject1", 50), new SubjectInfo("Subject2", 70) });
StudentGrid.DataContext = student;
Run Code Online (Sandbox Code Playgroud)
但此TwoWay绑定不起作用SubjectInfo(从 UI 更改值不会被代码隐藏捕获),
但如果将SubjectInfo 结构更改为SubjectInfo 类,则它可以工作。
无法理解为什么。请给我建议。
通过将 a 分配SubjectInfo给DataContext,它会被复制(因为它是 / struct)ValueType,这就是绑定不起作用的原因。在大多数情况下这不是您想要的。我建议你使用一个类来SubjectInfo代替。
class您应该了解和之间的差异struct。C # 规范 (V4.0)对它们进行了如下总结(第 11.3 节):
System.ValueType(\xc2\xa711.3.2)。null。object(\xc2\xa711.3.5) 之间进行转换。this对于结构 (\xc2\xa77.6.7),的含义有所不同。| 归档时间: |
|
| 查看次数: |
1895 次 |
| 最近记录: |