浏览包含在主窗口中的多个子视图 [英] Navigating through multiple child views contained in Main Window

查看:16
本文介绍了浏览包含在主窗口中的多个子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 WPF MVVM 应用程序,我需要一个带有徽标的主窗口,并且它必须在其中显示子视图.我在主窗口中没有任何控件,所有控件都驻留在子视图中,例如 Next、Back、Cancel 和一些文本块等按钮.现在如果用户在子视图上选择 Next 按钮,我必须绘制或加载下一个主窗口内的子视图.如果单击后退按钮,我必须返回上一个子视图.所以基本上我正在根据单击哪个按钮来更改子视图.此外,我为每个子视图维护不同的视图模型.现在的问题是我无法弄清楚我应该如何将子视图链接到各自的视图模型.这个应用程序类似于一些安装应用程序,根据选择和用户单击的按钮显示不同的对话框.我是这个 wpf 的新手,不想使用 MVVM Light、Prism 等.任何详细的帮助都会很大赞赏.提前致谢.

I am working on a an WPF MVVM application where I need to have a Main Window with just a logo and it has to show child views inside it. I don't have any controls in Main Window all the controls reside in child view for example Buttons like Next, Back, Cancel and some text blocks etc. Now If users select Next button on the child view I have to draw or load the next child view inside the Main Window. If Back button is clicked I have to go back to the previous child view. So basically I am changing the child views depending on which button is clicked. Also I am maintaining different view models for every child view. Now the problem is I am not able to figure how should I link the child views to there respective view models. This application is similar to some Installation applications where different dialogs are shown depending on the selection and the button clicked by the user.I am new to this wpf and don't want to use MVVM Light , Prism etc. Any detailed help will be greatly appreciated. Thanks in advance.

推荐答案

将任何数据类型与 XAML 控件关联的最简单方法之一是使用 DataTemplate.因此,您可以简单地将这样的内容添加到您的 Application.Resources 中,并且只要您 不要在 DataTemplate 上设置 x:Key 属性s,那么框架会在遇到视图模型的实例时显式应用它们:

One of the easiest ways to associate any data type with XAML controls is to use a DataTemplate. Therefore, you can simply add something like this into your Application.Resources and as long as you do not set the x:Key properties on the DataTemplates, then they will be explicitly applied by the Framework whenever it comes across instances of your view models:

<DataTemplate DataType="{x:Type ViewModels:HomeViewModel}">
    <Views:HomeView />
</DataTemplate>
...
<DataTemplate DataType="{x:Type ViewModels:MainViewModel}">
    <Views:MainView />
</DataTemplate>

然后显示视图就这么简单:

Then displaying the view is as simple as this:

<ContentControl Content="{Binding YourViewModelProperty"} />

在后面的代码或您的视图模型中:

In code behind, or your view model:

YourViewModelProperty = new MainViewModel();

为你的视图模型创建一个基类通常很方便,然后 YourViewModelProperty 可以是那种类型,你将能够使用相同的属性和 ContentControl 来交换它们>.

It's often handy to create a base class for your view models and then the YourViewModelProperty can of that type and you will be able to interchange them using the same property and ContentControl.

更新>>>

一般的想法是你有一个 MainViewModel 类,其中一个 BaseViewModel 属性数据绑定到 MainWindow.xaml 中的一个 ContentControl... 导航控件也应该在 MainWindow.xaml 中,而不是在视图本身中.这样,MainViewModel 类负责在接收到来自MainWindow.xamlCommands 时,将属性更改为相关的视图模型实例>.

The general idea is that you have one MainViewModel class with one BaseViewModel property data bound to one ContentControl in MainWindow.xaml... the navigation controls should also be in MainWindow.xaml and not in the views themselves. In this way, the MainViewModel class is responsible for changing the property to the relevant view model instances when it receives navigation Commands from the MainWindow.xaml.

这篇关于浏览包含在主窗口中的多个子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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