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>
If you use the normal
Binding.Pathis should bind to the properties of theDataContextobject which should be anXmlElementso just try{Binding Name}.