Bind XDocument to WPF and still use XPath?

不问归期 提交于 2020-01-13 19:25:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!