具有多个视图模型的MVVM [英] MVVM with multiple view models

查看:89
本文介绍了具有多个视图模型的MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主窗口,网格将窗口水平分成两半。



在上面的窗口中,我有几个按钮和一个数据网格,其中包含我正在搜索的目录。



底部窗口有一个清除按钮和一个显示搜索结果的数据网格视图。



如何为上部窗口声明SearchViewModel和XAML中底部窗口的ResultsViewModel?



据我所知,我只能在主窗口XAML中声明一个ViewModel。为什么我不能在XAML中为每个半部分声明数据上下文并将每个数据网格绑定到它们各自的ViewModel?



我必须遗漏一些基本的东西。



感谢您的帮助。

I have a main window with a grid dividing the window in half horizontally.

In the upper window I have a couple of buttons and a datagrid that holds the directories I'm searching.

The bottom window has a clear button and a datagrid view that displays the results of the search.

How can I declare the SearchViewModel for the upper window and the ResultsViewModel for the bottom window in XAML?

As far as I can tell I can only declare one ViewModel in the main window XAML. Why can't I have a data context declared in XAML for each of the halves and bind each datagrid to their respective ViewModel?

I must be missing something basic here.

Thanks for your help.

推荐答案

谁告诉你,你只能在一个窗口中创建一个视图(和它的XAML)?

这个CodeProject文章为你提供了多个简单的例子一个窗口中的视图:使用两个视图的MVVMLight [ ^ ](也许不是一篇非常好的文章,但对于讨论的目的)。



注意,我说的是多个视图,而不是多个视图模型。正如您尝试的那样,讨论视图模型和窗口之间的映射并不是一种有效的方法。视图模型不是视觉事物,视图是,所以首先在一个窗口中考虑不同的视图,这是合理的。毕竟,只有一个(非模态)主窗口的应用程序UI设计是最方便的一个。你并不是在暗示任何异常。



关于视图模型和视图之间的映射,这是一个单独的问题。尝试在本文中对多个视图和视图模型进行非常合理的讨论: http:// www.wintellect.com/devcenter/jlikness/model-view-viewmodel-mvvm-explained [ ^ ]。



作者可以说坚持认为没有必要附上多个视图模型到单个视图,但单个视图模型可能被多个视图使用 - 作为鸡蛋问题鸡的一部分。在这里,你也没有提出任何异常的建议。据我所知,您希望有一个用于搜索视图的视图模型和一个用于结果视图的视图模型。这听起来很合理。



-SA
Who told you that you can only create one view in a single window (and its XAML)?
This CodeProject article gives you the simple example of multiple views in one window: MVVMLight Using Two Views[^] (maybe not a very good article, but good enough for the purpose of out discussion).

Note, that I'm talking about multiple views, not multiple view models. Discussing mapping between view models and windows, as you are trying to do, is not a valid approach. View models are not visual things, views are, so first consider different views in one window, which is more than reasonable. After all, the application UI design with only one (non-modal) main window is one of the most convenient ones. You are not suggesting anything unusual.

As to the mapping between view models and views, this is a separate concern. Try to follow very reasonable discussion on multiple views and view models in this article: http://www.wintellect.com/devcenter/jlikness/model-view-viewmodel-mvvm-explained[^].

The author arguably insists that there is no need "to attach multiple viewmodels to a single view", but "a single viewmodel might be used by multiple views" — as part of "the chicken of the egg problem". Here, you also don't suggest anything unusual. As I could understand you, you want to have one view model for search view and one for the results view. It sounds quite reasonable.

—SA


据我了解,我只能申报XAML中的一个viewmodel实例?那么,我是否可以返回代码来创建实例?
As I understand it, I can only declare one instance of a viewmodel in the XAML? So, do I go to back code to create the instances?


这篇关于具有多个视图模型的MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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