本文介绍了XDocument之上的ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WPF应用程序,该应用程序具有表示XML的树状视图.我将XML加载到XDocument,然后将TreeView绑定到该对象.

I am working on a WPF application which has a treeview that represents an XML. I load the XML on to the XDocument, then bind the TreeView to this object.

现在使用 MVVM模式,我想要在 XDocument 上方提供一个 ViewModel .我应该在ViewModel类中实现哪些功能?

Now using the MVVM pattern, I want to provide a ViewModel on top of XDocument. What are some of the things that I should implement in the ViewModel class.

我在想,

  1. 绑定到TreeView上的ContextMenu命令的RoutedCommands,以允许添加节点,删除节点,修改节点等
  2. 在视图上实际修改属性和节点名的逻辑.

我朝着正确的方向前进吗?我还应该做些什么,以使其更简洁,模块化,同时又易于理解.

Am I going in the right direction? What else should I do to make it cleaner, modular at the same time easy to understand.

我应该使用RoutedCommands还是实现ICommand接口并创建命令?如何为CommandBindings使用附加属性?在我正在谈论的treeview应用程序中这样做有意义吗?由于实现此功能的选择太多,我有点不知所措.

Should I use RoutedCommands or implement ICommand interface and create my commands?How about using attached properties for CommandBindings? does it make sense to do it in the treeview app that I am talking about? I am a bit overwhelmed because of so many options available to implement this.

有人有链接,执行此类操作的示例代码吗?参考实现可能是?

Does anyone have links, sample code that does this sort of thing? A reference implementation may be?

推荐答案

我和你在一起.我从

(ui)<->(xml)

(ui) <-> (xml)

其中xml表示为LINQy XElements,所以我收到了PropertyChange通知.

where xml represented as LINQy XElements so I got PropertyChange notification.

然后,我添加了一些无状态的帮助器类来帮助我处理xml(公开属性,验证数据等).我将XElement捆绑在ObservableCollections中,以便可以绑定到它们.

I then added some stateless helper classes to help me deal with xml (expose properties, validate data, etc). I'd bundle up XElements in ObservableCollections so I could bind to them.

阅读有关M-V-VM的信息,并决定将我的助手类转换为ViewModels.问题:辅助类位于数据模型名称空间中,该名称空间对UI一无所知. Helper类知道如何将数据库行转换为XElement,ViewModel应该永远不会看到.助手类处理xml. ViewModel不应该知道或不在乎.

Read about M-V-VM, and decided to convert my helper classes into ViewModels. Problems:Helper classes live in the data model namespace which knows nothing about UI. Helper classes know how to convert database row into XElement, ViewModel should never see that.Helper classes deal with xml. ViewModel shouldn't know or care.

所以我实际上正在考虑实施

So I'm actually considering implementing

(ui)<->(viewmodel)<->(helper)<->(xml)

(ui) <-> (viewmodel) <-> (helper) <-> (xml)

但是我只是不赞成在助手中引发PropChange事件,只是在视图模型中重新引发它们.

but i just balk at raising PropChange events in helper only to reraise them in viewmodel.

这篇关于XDocument之上的ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 13:21