Im a newbie to xml as well as WPF. I have an xml file as shown:
我是xml以及WPF的新手。我有一个xml文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Listofattributes>
<Attribute name="Patient Name" Value="John"/>
<Attribute name="Sex" Value ="female"/>
</Listofattributes>
Im trying to load the values into the listbox in a WPF. Please help me out with how i could do it. Tried reading many articles but i couldn't quite figure it out. Thanks!
我试图将值加载到WPF的列表框中。请帮我解决一下我是怎么做到的。尝试阅读许多文章,但我无法弄明白。谢谢!
1
You can use an XmlDataProvider to refer to a separate file that contains the data as in:
您可以使用XmlDataProvider来引用包含数据的单独文件,如下所示:
<XmlDataProvider x:Key=”OrgChartData” Source=”orgchart.xml” XPath=”Sex”/>
OR
要么
<XmlDataProvider x:Key=”regions” XPath=”Regions”>
<x:XData>
<Regions xmlns=””>
<Region RegionName=”East”>
...
</Region>
<Region RegionName=”Central”>
...
</Region>
</Regions>
</x:XData>
</XmlDataProvider>
then determ yor DateTemplate of ListBox.ItemTemplate, which will have Binding like this:
然后确定ListBox.ItemTemplate的DateTemplate,它将具有如下的Binding:
<DataTemplate DataType=”Region” ItemsSource=”{Binding XPath=*}”>
<TextBlock Text=”{Binding XPath=@RegionName}” />
</DataTemplate>
And your ListBox.ItemsSource should be ="{StaticResource regions}".
而你的ListBox.ItemsSource应该是=“{StaticResource regions}”。
If you have separate file, you can write next lines
如果您有单独的文件,则可以编写下一行
<XmlDataProvider x:Key=”regions” Source=”Regions.xml” XPath=”Regions”/>
0
or programmatically:
或以编程方式:
void form_Loaded(object sender, RoutedEventArgs e)
{
XElement xml = XElement.Parse(string.Join("", File.ReadAllLines("XMLFile1.xml")));
foreach (XElement el in xml.Elements())
{
ListBoxItem item = new ListBoxItem();
string name = el.Attribute("name").Value;
string value = el.Attribute("Value").Value;
item.Content = name + ": " + value;
ListBox.Items.Add(item);
}
}
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2011/08/17/410fc45724f2f909ed9edbfc227d2278.html。