问题描述
我的XML数据类似于下面的例子,我试图将它绑定到一个ListView。我无法绑定元素名称,这是该示例中的汽车品牌。我从这篇文章中发现了那xaml不支持xpath函数名。因此,local-name()不起作用。但是要做到这一点...
I have XML data similar to the example below and I am trying to bind it to a ListView. I am having trouble binding the element name, which is the brand of the car in the example. I have found out from this post Xaml Support for Local Name in XPath that xaml doesn't support xpath function names. Therefore, local-name() doesn't work. But there got to be a way to do this...
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Height="350"
Width="525">
<Window.Resources>
<XmlDataProvider x:Key="DataSource">
<x:XData>
<Cars xmlns="">
<Data>
<Honda Year="2012"
Color="Red"
Model="Accord" />
<Subuar Year="2008"
Color="Blue"
Model="Outback" />
<Ford Year="2000"
Color="Black"
Model="Focus" />
</Data>
</Cars>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<ListView ItemsSource="{Binding XPath=Cars/Data/*}"
DataContext="{StaticResource DataSource}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding XPath=???}"
Header="Brand" />
<GridViewColumn DisplayMemberBinding="{Binding XPath=@Year}"
Header="Year" />
<GridViewColumn DisplayMemberBinding="{Binding XPath=@Color}"
Header="Color" />
<GridViewColumn DisplayMemberBinding="{Binding XPath=@Model}"
Header="Model" />
</GridView>
</ListView.View>
</ListView>
</Window>
推荐答案
如果您使用正常的 Binding.Path
应该绑定到应该是,所以只需尝试 {绑定名称}
。
If you use the normal Binding.Path
is should bind to the properties of the DataContext
object which should be an XmlElement
so just try {Binding Name}
.
这篇关于如何将xml元素名称绑定到WPF XAML文件中的ListView列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!