[英]How to bind the xml element name to a ListView column in WPF XAML file
I have XML data similar to the example below and I am trying to bind it to a ListView. 我有类似于下面的例子的XML数据,我试图将它绑定到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.
我从XPath中的Xaml支持本地名称中发现xaml不支持xpath函数名称。 Therefore, local-name() doesn't work.
因此,local-name()不起作用。 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>
I don't think you can bind to an element name, is would require the "name()" or "local-name()" XPath functions, but they aren't supported in Xaml. 我认为你不能绑定到元素名称,需要“name()”或“local-name()”XPath函数,但Xaml不支持它们。 You can work around it, see this URL:
您可以解决此问题,请参阅此URL:
XAML support for local-name() in XPath XAML支持XPath中的local-name()
But this does work and isn't as ugly as a hack-ar, just requires a slightly different XML input: 但这确实有效,并不像hack-ar那样丑陋,只需要稍微不同的XML输入:
<Window.Resources>
<XmlDataProvider x:Key="DataSource">
<x:XData>
<Cars xmlns="">
<Data>
<Car Brand="Honda" Year="2012" Color="Red" Model="Accord"/>
<Car Brand="Subuaru" Year="2008" Color="Blue" Model="Outback"/>
<Car Brand="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=@Brand}" 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>
如果你使用普通的Binding.Path
应该绑定到DataContext
对象的属性,该对象应该是一个XmlElement
所以只需尝试{Binding Name}
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.