0

我不确定我做错了什么,但我的 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>;
        }
4

1 回答 1

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);
}
于 2013-06-18T06:03:25.397 回答