使用MVVM,多视图如何共享一个视图模型 [英] With MVVM,how is one viewmodel shared by multi-views
本文介绍了使用MVVM,多视图如何共享一个视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
作为标题,我想要一个由几个视图共享的视图模型。我只想要在内存中创建一个副本,使每个视图都可以使用相同的属性。
As the title, I want one viewmodel shared by Several views. And I just want a copy created in the memory,sothat each view can use the same property.
推荐答案
你的问题很广泛,缺乏很多细节。所以我不能回答问题,但我可以告诉你我想要做什么。
如果多个视图属于同一类型,例如Visual Studio中的C#编辑器。
我会在我的主ViewModel中公开CsharpDocuments
的集合,例如ObservableCollection< CsharpDocument>
然后将该集合绑定到ItemsControl
。
如果视图的类型不同,我会在mainViewModel中为这些视图的每个ViewModel创建一个属性,然后将视图的DataContext绑定到相应的属性。
Your question is very broad, and lacks a lot of details. So I cannot answer question, but I can give tell you want I would do.
If the multiple views are of the same type, e.g. C# editor in Visual Studio.
I would in my "main" ViewModel expose a collection ofCsharpDocuments
e.g.ObservableCollection<CsharpDocument>
and then bind that collection to anItemsControl
.
If the views are of different type I would have a property in my "main" ViewModel for each ViewModel for those views and then bind the DataContext of the views to the appropriate property.
MVVM Light工具包将执行您想要的操作。 Laurent在他的样本中使用的ViewModel定位器可以解决问题!
http: //www.galasoft.ch/mvvm
The MVVM Light toolkit will do what you want. The ViewModel Locator that Laurent uses in his samples will do the trick!
http://www.galasoft.ch/mvvm
使用以下条目查看:
Use following for your views :
this.DataContext = ViewModelName();
you可以在很多页面中访问相同的视图模型。
-Sagar Solanki
you can access the same view-model in many pages.
-Sagar Solanki
这篇关于使用MVVM,多视图如何共享一个视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文