[英]How can I switch views between viewmodels?
My Main window defines the markup for the application, for this specific scenario lets say I have a grid with 2 columns. 我的主窗口定义了应用程序的标记,对于这种特定情况,可以说我有一个包含2列的网格。
First column will have navigation links, and second column will display the different views. 第一列将具有导航链接,第二列将显示不同的视图。
There are 2 views (and 2 viewmodels) defined in mainwindow xaml: 在mainwindow xaml中定义了2个视图(和2个视图模型):
<Window.Resources>
<DataTemplate DataType="{x:Type vm:Window1ViewModel}">
<vw:Window1View/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:Window2ViewModel}">
<vw:Window2View/>
</DataTemplate>
</Window.Resources>
And in second grid column that displays the views i got : 在第二个显示我得到的视图的网格列中:
<ContentControl Content="{Binding Path=ViewModel}" HorizontalAlignment="Left">
</ContentControl>
Where ViewModel is a property that I set accordingly to a view(viewmodel) that i want to display. 其中ViewModel是我要设置为要显示的view(viewmodel)的属性。
Like : 喜欢 :
ViewModel = new Window1ViewModel(); ViewModel =新的Window1ViewModel();
(datacontext of the mainwindowview is set to MainWindowViewModel) (将mainwindowview的datacontext设置为MainWindowViewModel)
So there is no problem to switch between views from the MainWindowViewModel. 因此,在MainWindowViewModel的视图之间切换是没有问题的。
My problem is how to switch within Window1ViewModel into Window1ViewMode2? 我的问题是如何在Window1ViewModel内切换到Window1ViewMode2?
The various ViewModels don't "know" about other ViewModels. 各种ViewModel都不“了解”其他ViewModel。
Only MainWindowViewModel knos about others... 只有MainWindowViewModel知道其他人...
How can I solve this? 我该如何解决? Maybe I should define a custom Event (with parameter), MainWindowViewModel will subscribe and other viewmodels will trigger it and then MainWindowViewModel will switch to the needed view? 也许我应该定义一个自定义事件(带有参数),MainWindowViewModel将订阅,其他视图模型将触发它,然后MainWindowViewModel将切换到所需的视图?
the solution you describe is one possibility. 您描述的解决方案是一种可能性。 One other I can think of is using some kind of Navigation-Service (static class or interface you pass to all your child -Viewmodels) that do this kind of work. 我能想到的另一个方法是使用某种导航服务(静态类或传递给所有子视图类的接口-Viewmodels)来完成这种工作。
If your MainWindowViewModel creates all the others I would stick to the interface solution. 如果您的MainWindowViewModel创建了所有其他窗口,我将坚持使用界面解决方案。 You can for example let the MainWindowVM implement such a interface and inject it into all the child-vm on creation. 例如,您可以让MainWindowVM实现这样的接口,并在创建时将其注入所有child-vm中。 This is much the same as your event-approach but instead of the childs-providing and the main having to subscribe you have the main give something ... IMHO the better approach. 这与您的事件处理方法几乎相同,但是除了提供子项和主要订阅服务外,您还可以主要提供一些东西……恕我直言,更好的方法。
Ok, may be I understood your point. 好吧,也许我理解您的意思了。 You want that controller actually be modelview which notifies to mainmodelview about the fact that it have to be swapped with someone else. 您希望该控制器实际上是modelview,它将必须与其他人交换的事实通知mainmodelview。
Considering that we are talking about WPF, create DependecyProperty on mainmodelview , and set it from childview, which in code behind will trigger modelviews swap. 考虑到我们正在谈论WPF,请在mainmodelview上创建DependecyProperty,然后从childview进行设置,这在后面的代码中将触发modelviews交换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.