如何在视图模型之间切换视图?
我的主窗口定义了应用程序的标记,对于这个特定场景,假设我有一个包含 2 列的网格。
第一列将有导航链接,第二列将显示不同的视图。
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>
在显示我得到的视图的第二个网格列中:
<ContentControl Content="{Binding Path=ViewModel}" HorizontalAlignment="Left">
</ContentControl>
其中 ViewModel 是我根据要显示的视图(viewmodel)设置的属性。
就像:
ViewModel = new Window1ViewModel();
(mainwindowview的datacontext设置为MainWindowViewModel)
所以从MainWindowViewModel切换视图是没有问题的。
我的问题是如何在 Window1ViewModel 内切换到 Window1ViewMode2?
各种 ViewModel 不“了解”其他 ViewModel。
只有 MainWindowViewModel 知道其他...
我该如何解决这个问题? 也许我应该定义一个自定义事件(带参数),MainWindowViewModel 将订阅并且其他视图模型将触发它,然后 MainWindowViewModel 将切换到所需的视图?
My Main window defines the markup for the application, for this specific scenario lets say I have a grid with 2 columns.
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:
<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.
Like :
ViewModel = new Window1ViewModel();
(datacontext of the mainwindowview is set to MainWindowViewModel)
So there is no problem to switch between views from the MainWindowViewModel.
My problem is how to switch within Window1ViewModel into Window1ViewMode2?
The various ViewModels don't "know" about other ViewModels.
Only MainWindowViewModel knos about others...
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您描述的解决方案是一种可能性。我能想到的另一种方法是使用某种导航服务(传递给所有子视图模型的静态类或接口)来完成此类工作。
如果您的 MainWindowViewModel 创建了所有其他模型,我将坚持使用界面解决方案。例如,您可以让 MainWindowVM 实现这样的接口,并将其注入到创建时的所有子虚拟机中。这与您的事件方法非常相似,但不是提供子项并且主要必须订阅,而是主要提供一些东西......恕我直言,这是更好的方法。
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.
If your MainWindowViewModel creates all the others I would stick to the interface solution. You can for example let the MainWindowVM implement such a interface and inject it into all the child-vm on creation. 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.
好吧,也许我明白你的意思了。您希望该控制器实际上是模型视图,它通知主模型视图它必须与其他人交换的事实。
考虑到我们正在讨论 WPF,在 mainmodelview 上创建 DependecyProperty ,并从 childview 中设置它,这在后面的代码中将触发 modelview 交换。
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.
Considering that we are talking about WPF, create DependecyProperty on mainmodelview , and set it from childview, which in code behind will trigger modelviews swap.