通过多个子导航包含在主要的窗口视图 [英] Navigating through multiple child views contained in Main Window

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

问题描述

我工作的一个MVVM WPF应用程序,我需要有一个主窗口只是一个标志,它表明里面的孩子的意见。我没有在主窗口的所有控件位于例如按钮的下一子视图任何控制,返回,取消和一些文字块等,现在如果用户选择子视图下一步按钮我要绘制或加载下一个在主窗口内子视图。如果单击后退按钮我必须回到以前的子视图。所以基本上我改变取决于被点击的按钮子视图。另外,我保持不同的视图模型为每个孩子视图。现在的问题是我不能够计算应该怎么链接的子视图有相应的视图模型。这个应用程序类似于安装一些应用中,不同的对话框会根据选择,并由user.I点击的按钮显示是新来这个WPF和不希望使用MVVM光,棱镜等任何详细的帮助将是很大的赞赏。先谢谢了。

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 键,只要你的不设置 X:键的DataTemplate 取值的,那么他们将被明确每当说到你的视图模型的实例之间的框架应用性能:

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 绑定到一个 ContentControl中有一个 BaseViewModel 属性数据类MainWindow.xaml ...导航控制也应在 MainWindow.xaml ,而不是在他们自己的看法。通过这种方式,在 MainViewModel 类负责当它接收到导航更改属性相关视图模型实例命令 ■从 MainWindow.xaml

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天全站免登陆