如何绑定到XmlDataProvider.Source财产MVVM [英] How to bind XmlDataProvider.Source to MVVM property

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

问题描述

我已经有了 XmlDataProvider 下面绑定到一个TreeView <一个href=\"http://stackoverflow.com/questions/188001/two-way-binding-of-xml-data-to-the-wpf-treeview\">this例如的。我工作的这个应用程序是继MVVM模式和XML是用户将打开一个文件。

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.

当我尝试的来源属性绑定的 XmlDataProvider 像这样

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

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

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

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

这么短补鞋一起在程序绑定是那里声明绑定 XmlDataProvider 来源办法?如果我尝试放弃数据提供程序和树直接绑定到的XmlNode 属性我得到使用XPath只处理XML对象绑定的错误;这使得完全没有意义,我,但我敢肯定它是想告诉我一些重要的东西。

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.

推荐答案

答案似乎是:你不能

我能解决我的根本问题通过删除(绑定一个TreeView到一个XML文件) XmlDataProvider 从方程和TreeView控件直接绑定到一个ViewModel属性,返回一个XmlNode。

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.

曾经是绊倒我,是我拿了结合code,它指出在 XmlDataProvider ,并指出它在我的财产,留在原位的XPath参数

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=.}">

这将导致运行时错误: System.Windows.Data错误:44:使用XPath BindingEx pression不能绑定到非XML对象; XPath的=。

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

这是不是最有帮助的。这是什么真正想说的是,你不能绑定到的XmlNode 属性,并提供一个的XPath 论点绑定(因为它是一个知道如何处理做??的XmlDataProvider)。

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天全站免登陆