在WPF中将ComboBox绑定到XML [英] ComboBox Binding to XML in WPF
问题描述
我知道这个问题已经死了,但是我尝试了很多发现的建议答案,当我在VS2013中启动WPF时,组合框仍然没有出现。来了我有一个名为People.xml的XML文档,其格式如下...
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. Here it goes. I have an XML document called People.xml that is formatted like so...
<?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>
在应用程序的App.xml部分中,我将此作为资源:
In App.xml portion of the application I have this as a resource:
<XmlDataProvider x:Key="People" Source="\DataSources\People.xml" XPath="People" IsInitialLoadEnabled="True" />
然后,在组合框的XAML中,我将其列出为:
Then, in the XAML for the combo box, I have it listed as this:
<ComboBox x:Name="employeeNameBox" IsReadOnly="False" HorizontalAlignment="Left" IsEditable="True" ItemsSource="{Binding Source={StaticResource People}, XPath=./Person/personName}">
我要了解的是用XML中的所有personName元素填充组合框
What I am trying to get at is populate the combo box with all of the personName elements in the XML doc.
再次,我尝试了几种不同的方法来尝试加载此方法,并且组合框始终显示为空。一般来说,我对数据绑定结构和WPF还是比较陌生的,所以我能得到的任何帮助都会很棒。
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.
谢谢!
推荐答案
对我来说,最简单的方法是 XmlSerializer
。
As for me, simpliest way is XmlSerializer
.
public class Person
{
public string personName;
public string personEmail;
public string personReports;
}
public class People
{
[XmlElement("Person")]
public List<Person> Persons;
}
加载数据:
var people = (People)new XmlSerializer(typeof(People)).Deserialize(stream);
employeeNameBox.ItemsSource = people.Persons;
组合框代码:
<ComboBox x:Name="employeeNameBox" IsReadOnly="False" HorizontalAlignment="Left" IsEditable="True" DisplayMemberPath="personName">
这篇关于在WPF中将ComboBox绑定到XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!