[英]ComboBox Binding to XML in WPF
I know this question has been asked to death, but I have tried lots of the suggested answers I have found and the Combo Box is still not populating when I start the WPF in VS2013. 我知道这个问题已经死了,但是我尝试了很多发现的建议答案,当我在VS2013中启动WPF时,组合框仍然没有出现。 Here it goes.
来了 I have an XML document called People.xml that is formatted like so...
我有一个名为People.xml的XML文档,其格式如下:
<?xml version="1.0" encoding="utf-8"?>
<People>
<Person>
<personName>John Doe</personName>
<personEmail>someone@yahoo.com</personEmail>
<personReports>List of reports they get go here.</personReports>
</Person>
In App.xml portion of the application I have this as a resource: 在应用程序的App.xml部分中,我将此作为资源:
<XmlDataProvider x:Key="People" Source="\DataSources\People.xml" XPath="People" IsInitialLoadEnabled="True" />
Then, in the XAML for the combo box, I have it listed as this: 然后,在组合框的XAML中,我将其列出为:
<ComboBox x:Name="employeeNameBox" IsReadOnly="False" HorizontalAlignment="Left" IsEditable="True" ItemsSource="{Binding Source={StaticResource People}, XPath=./Person/personName}">
What I am trying to get at is populate the combo box with all of the personName elements in the XML doc. 我想要了解的是在组合框中填充XML文档中的所有personName元素。
Again, I have tried several different ways to try and get this to load, and the combo box always comes up empty. 再次,我尝试了几种不同的方法来尝试加载它,并且组合框始终显示为空。 I am relatively new to the data binding structures and WPF's in general, so any help I could get would be great.
我对数据绑定结构和WPF相对较新,所以我能得到的任何帮助都会很棒。
Thanks! 谢谢!
As for me, simpliest way is XmlSerializer
. 对于我来说,最简单的方法是
XmlSerializer
。
public class Person
{
public string personName;
public string personEmail;
public string personReports;
}
public class People
{
[XmlElement("Person")]
public List<Person> Persons;
}
Load data: 加载数据:
var people = (People)new XmlSerializer(typeof(People)).Deserialize(stream);
employeeNameBox.ItemsSource = people.Persons;
ComboBox code: ComboBox代码:
<ComboBox x:Name="employeeNameBox" IsReadOnly="False" HorizontalAlignment="Left" IsEditable="True" DisplayMemberPath="personName">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.