views:

437

answers:

1

I've got a treeview bound to an XmlDataProvider following this example. The app I am working on is following the MVVM pattern and the Xml is from a file that the user will open.

When I try to bind the Source property of the XmlDataProvider like so

<XmlDataProvider Source="{Binding Path=XmlFilePath}"/>

I get a "Binding can only be applied to a DependencyProperty of a Dependency object." or somesuch.

So short of cobbling the binding together procedurally is there a way to declaratively bind the XmlDataProvider Source? If I try to forgo the data provider and bind the tree directly to an XmlNode property I get an error about using XPath binding only with Xml objects; which makes absolutely no sense to me but I'm sure it's trying to tell me something important.

+1  A: 

The answer appears to be: you can't.

I was able to solve my underlying problem (binding a treeview to an Xml document) by removing the XmlDataProvider from the equation and binding the TreeView directly to a ViewModel property that returns an XmlNode.

What had been tripping me up was that I took the binding code that pointed at the XmlDataProvider and pointed it at my property, leaving the XPath argument in place.

<TreeView ItemsSource="{Binding Path=ProjectDocument XPath=.}">

This would result in a runtime error: System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='.'

Which was not the most helpful. What it was really trying to say is that you can't bind to an XmlNode property AND provide an XPath argument in the binding (because it's the XmlDataProvider that knows what to do with that??).

<TreeView ItemsSource="{Binding Path=ProjectDocument}">
dkackman