我不确定我做错了什么,但我的dataGrid不会填充。 我花了一个小时来调试linq,但我发现它确实在工作......
foreach (XElement elm in xDocument.Element("ArrayOfPopulation").Elements("Population")) {
Console.Write(elm.Element("id").Value+ " | ");
Console.Write(elm.Element("name").Value+ " | ");
Console.WriteLine(elm.Element("description").Value);
}
上面的代码显示我实际上是从linq中获取值...
3 | CHF | FR Congestive Heart Failure
2 | COPD | FR Chronic Obstructive Pulmonary Disease
但是低于我的模型是空的?我无法弄清楚它可能是什么?这不像我得到空值。我不认为它与wpf数据绑定有关,但我很难理解它可能是什么。
try {
XElement elem = xDocument.Element("ArrayOfPopulation");
popModel =
xDocument
.Element("ArrayOfPopulation")
.Elements("Population")
.Select(template => new PopulationModel {
populationID = template.Element("id").Value,
PopName = template.Element("name").Value,
description = template.Element("description").Value,
populationType = template.Element("owner").Element("type").Value,
isActive = Boolean.Parse(template.Element("isActive").Value)
})as ObservableCollection<PopulationModel>;
}
答案 0 :(得分:1)
我认为你不能投射到可观察的集合......你需要将你的集合传递给ObservableCollection的构造函数
try {
XElement elem = xDocument.Element("ArrayOfPopulation");
popModel =
xDocument
.Element("ArrayOfPopulation")
.Elements("Population")
.Select(template => new PopulationModel {
populationID = template.Element("id").Value,
PopName = template.Element("name").Value,
description = template.Element("description").Value,
populationType = template.Element("owner").Element("type").Value,
isActive = Boolean.Parse(template.Element("isActive").Value)
});
var popModelCollection = new ObservableCollection<PopulationModel>(popModel);
}