开发者

Displaying an XDocument as a WPF treeview using an MVVM ViewModel

I have XSD's held as data in a SQL Server XML, or oracle XMLtype field. I retrieve an XSD from the database as an XDocument. I want to display this XDocument in a WPF treeview. This is relatively straight forward if I bind the Xdocument directly to the treeview using an xmldataprovider and a hierarchicalDataTemplate.

However, my appli开发者_如何学JAVAcation is using the mvvm pattern and I would like to represent the xdocument in the ViewModel layer, which the treeview then binds to in a similar way to the Josh Smith article on binding the WPF treeview to ViewModels. http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx

My goal is to select an XNode of the XSD (XDocument).

Any suggestions for how I could represent the Xdocument of an XSD in the ViewModel so that I can select an XNode?


If you're doing read-only operations, I'd recommend that you keep it simple and don't introduce new classes that provide no benefit. Just leave a comment that you should add a view model if you ever want to support editing the structure. Even if you want to be able to edit the document, you might still be able to bind to the document and nodes directly, depending on your needs.

If you need more advanced support (like INotifyPropertyChanged), I suggest that you create a hierarchy of classes like the original API, based on XObject. I would only add properties that I planned on directly supporting in the UI. I would then create a separate model class that could convert from the XDocumentViewModel hierarchy to a real XDocument, and back.

... how [to] represent the Xdocument of an XSD in the ViewModel so that I can select an XNode.

What do you mean by select?

If you mean in the UI, then that's taken care of in the XAML.

If you're talking about querying the view model, then you could try to use the existing LINQ XML querying API against the underlying document. Keep an internal dictionary mapping XObjects back to your view model objects, and when you get results, simply look up each result in the dictionary before returning it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜