如何将 XmlDataProvider.Source 绑定到 MVVM 属性 [英] How to bind XmlDataProvider.Source to MVVM property

查看:30
本文介绍了如何将 XmlDataProvider.Source 绑定到 MVVM 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 这个例子.我正在开发的应用程序遵循 MVVM 模式,Xml 来自用户将打开的文件.

当我尝试像这样绑定 XmlDataProvider 的 Source 属性时

我得到一个绑定只能应用于依赖对象的 DependencyProperty".或者诸如此类.

除了在程序上将绑定拼凑在一起之外,有没有办法以声明方式绑定XmlDataProvider Source?如果我尝试放弃数据提供程序并将树直接绑定到 XmlNode 属性,我会收到有关仅将 XPath 绑定用于 Xml 对象的错误;这对我来说完全没有意义,但我敢肯定它是想告诉我一些重要的事情.

解决方案

答案似乎是:你不能.

通过从等式中删除 XmlDataProvider 并将 TreeView 直接绑定到返回 XmlNode 的 ViewModel 属性,我能够解决我的潜在问题(将树视图绑定到 Xml 文档).

让我感到困惑的是,我将指向 XmlDataProvider 的绑定代码指向我的属性,而将 XPath 参数保留在原位.

这将导致运行时错误:System.Windows.Data Error: 44 : BindingExpression with XPath 无法绑定到非 XML 对象.;XPath='.'

这不是最有帮助的.它真正想说的是,您不能绑定到 XmlNode 属性并在绑定中提供 XPath 参数(因为 XmlDataProvider 知道要做什么)那个??).

I've got a treeview bound to an XmlDataProvider following this example. The app I am working on is following the MVVM pattern and the Xml is from a file that the user will open.

When I try to bind the Source property of the XmlDataProvider like so

<XmlDataProvider Source="{Binding Path=XmlFilePath}"/>

I get a "Binding can only be applied to a DependencyProperty of a Dependency object." or somesuch.

So short of cobbling the binding together procedurally is there a way to declaratively bind the XmlDataProvider Source? If I try to forgo the data provider and bind the tree directly to an XmlNode property I get an error about using XPath binding only with Xml objects; which makes absolutely no sense to me but I'm sure it's trying to tell me something important.

解决方案

The answer appears to be: you can't.

I was able to solve my underlying problem (binding a treeview to an Xml document) by removing the XmlDataProvider from the equation and binding the TreeView directly to a ViewModel property that returns an XmlNode.

What had been tripping me up was that I took the binding code that pointed at the XmlDataProvider and pointed it at my property, leaving the XPath argument in place.

<TreeView ItemsSource="{Binding Path=ProjectDocument XPath=.}">

This would result in a runtime error: System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='.'

Which was not the most helpful. What it was really trying to say is that you can't bind to an XmlNode property AND provide an XPath argument in the binding (because it's the XmlDataProvider that knows what to do with that??).

<TreeView ItemsSource="{Binding Path=ProjectDocument}">

这篇关于如何将 XmlDataProvider.Source 绑定到 MVVM 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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