问题
This is a 2 part question.
1) Is it possible to bind XDocument to a WPF control without using ObjectDataProvider ?
Here is a snippet of my code in which XmlDocument works, but i cannot use XDocument
XmlDataProvider provider = new XmlDataProvider();
provider.XPath = "/Parent/Child";
provider.Document = mydoc; // xmldocument works fine.
Binding binding = new Binding();
binding.XPath = "InnerChild/Name";
binding.Source = provider;
decisionCb.SetBinding(ComboBox.ItemsSourceProperty, binding);
I need to retain the ability to bind using an XPath because my Xml document is generated on the fly. I wanted to use XDocument and LINQ :(
2) Is it possible to use XPath extension functions in XElement within Xaml ?
<DataTemplate DataType="{}{http://myns}Child" >
<StackPanel Orientation="Horizontal">
<!-- This wont work because Element cannot accept XPath -->
<TextBlock Text="{Binding Path=Element[{http://myns}InnerChild/Name]}" />
</StackPanel>
</DataTemplate>
回答1:
The answer to the first question is "yes, you can".
One good article about this argument is the Beatrix Stollniz' one: link text
About the second answer, I say "no, it is not possible". XLinq architecture is built taking advantage from the IEnumerable (basic Linq) extensions, so XPath would be unnecessary and (let's say) not-so-adequate.
Cheers
来源:https://stackoverflow.com/questions/1665360/bind-xdocument-to-wpf-and-still-use-xpath