WPF绑定属性Path和XPath是否互斥? [英] Are WPF binding properties Path and XPath mutually exclusive?

查看:2139
本文介绍了WPF绑定属性Path和XPath是否互斥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个 UserControl ,其 DataContext 设置为一个具有 XmlDataProvider 属性。我想绑定到我的控件的XAML中的这个属性,并指定一些XPath。我试过这个:

Suppose I have a UserControl whose DataContext is set to an object that has an XmlDataProvider property. I would like to bind to this property in my control's XAML, and specify some XPath. I tried this:

<TreeView ItemsSource="{Binding Path=PropertyName, XPath=/items/item/*}">

在运行时我得到异常使用XPath的BindingExpression无法绑定到非XML对象。删除 Path 属性并将 TreeView 的DataContext设置为 XmlPropertyName 直接工作正常。

At runtime I get the exception "BindingExpression with XPath cannot bind to non-XML object". Removing the Path property and setting the TreeView's DataContext to the XmlPropertyName directly works fine.

什么给了? 路径 XPath 互斥?

推荐答案

它们不是相互排斥的,如 指定价值路径部分的数据绑定概述
XPath表达式将在Path属性之前执行,但有时候可以使用Bea Stollnitz来解释这里这里

They are not mutually exclusive, as briefly mentioned in the Data Binding Overview under the Specifying the Path to the Value section. The XPath expression will be executed before the Path property, but is sometimes desirable to use as Bea Stollnitz explains here and here.


删除Path属性并将TreeView的DataContext设置为XmlPropertyName直接工作正常。

"Removing the Path property and setting the TreeView's DataContext to the XmlPropertyName directly works fine."

既然你提到这个,在我看来,就像你期望在XPath表达式之前评估Path属性,这就是为什么你得到一个错误,XPath表达式正在评估你的任何当前环境是,并且之后正在评估路径。
错误说明

Since you mentioned this, it seems to me like you are expecting the Path property to be evaluated before the XPath expression, which would be why you are getting an error, the XPath expression is evaluating on whatever your current context is, and the path is being evaluated after that. error explanation

这篇关于WPF绑定属性Path和XPath是否互斥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆