将XDocument绑定到WPF并仍使用XPath? [英] Bind XDocument to WPF and still use XPath?
问题描述
这是一个两部分的问题。
This is a 2 part question.
1)是否可以在不使用ObjectDataProvider的情况下将XDocument绑定到WPF控件?
1) Is it possible to bind XDocument to a WPF control without using ObjectDataProvider ?
这是XmlDocument可以在其中工作的代码段,但是我不能使用XDocument
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);
我需要保留使用XPath进行绑定的功能,因为我的Xml文档是动态生成的。我想使用XDocument和LINQ:(
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)是否可以在Xaml中的XElement中使用XPath扩展功能?
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>
推荐答案
第一个问题的答案是是的,您Beatrix Stollniz的文章就是一篇有关此论点的好文章:链接文本
关于第二个答案,我说不,不可能。 XLinq体系结构是利用IEnumerable(基本Linq)扩展的优势而构建的,因此XPath是不必要的,并且(还可以说)不够。
干杯
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
这篇关于将XDocument绑定到WPF并仍使用XPath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!