Search code examples
wpfbindingmvvmtreeviewxmldataprovider

Binding against the XmlDataProvider fails?


I have some Problems with Binding of a XmlDataProvider to the WPF TreeView.

The TreeView is XAML-Defined like this:

<TreeView ItemsSource="{Binding Path=SelectedSystemVersionHistory}"/>

and I have a HierarchicalDataTemplate in the Parent Grid Resource of the TreeView for the Nodes in the XML File:

<HierarchicalDataTemplate DataType="Documentation" ItemsSource="{Binding XPath=*}">
  <TextBlock Text="{Binding XPath=@SoftwarePackage}"/>
</HierarchiclaDataTemplate>

My ViewModel has this Property:

public XmlDataProvider SelectedSystemVersionHistory
{
  get
  {
    String file = GetHistoryFile(); //returns full Filepath
    return new XmlDataProvider()
    {
      source = new Uri(file, UriKind.Absolute),
      XPath= "History"
    };
  }
}

And the Xml looks like this:

<?xml version="1.0" standalone="yes" encoding="utf-8">
  <History>
    <Documentation SoftwarePackage="SoftwarePackageName">
      <Entry>...</Entry>
    </Documentation>
  </History>

The Problem is the TreeView doesn't show anything, so what is wrong? I'm working on this for days now... :o( thank u for your help.


Solution

  • Unfortunately you can't bind the Document and Source properties of the XmlDataProvider directly, they aren't DependencyProperties. See also How to bind XmlDataProvider.Source to MVVM property

    What you can do is assign the Treeview's DataContext to the XMLDataProvider:

    <TreeView DataContext="{Binding SelectedSystemVersionHistory}" ItemsSource="{Binding XPath=Documentation}"/>