I have an XML document that describes how to build UI elements for user input, and I have data objects that contain some data and an XPath expression. I have a DataTemplate for the data object type that uses a HierarchicalDataTemplate to build the UI based on the XML, but I only want to use a subset of the XML. If the XPath expression were static, I could just write:
<TreeView ItemsSource="{Binding Source={StaticResource dataProvider},
XPath=/Static/XPath/Expression/*}" />
Since the XPath expression comes from a data object, it would be convenient to use data binding:
<TreeView ItemsSource="{Binding Source={StaticResource dataProvider},
XPath={Binding Path=Data.XPathExpression}}" />
Unfortunately, the Binding MarkupExtension doesn’t inherit from DependencyObject, so it’s properties aren’t DependencyProperty and don’t support data binding.
How can I apply a dynamic XPath expression when binding to XML data?
You can create a IMultiValueConverter that converts the XML data and XPath expression into new XML data:
Then, use MultiBinding to bind the ItemsSource to the XML data and a dynamic XPathExpression with the converter: