So I am doing my first WPF MVVM app. Just learning the right principle of MVVM, but there are some things that I don't understand ...
所以我正在做我的第一个WPF MVVM应用程序。刚学习MVVM的正确原则,但有一些我不明白的东西......
I already have several user controls defined. First question is what is better to use, UserControl or DataTemplates to change content of the MainWindow?
我已经定义了几个用户控件。第一个问题是什么更好用,UserControl或DataTemplates来改变MainWindow的内容?
And how to make "Binding" in the "MainWindow.xaml" to change UserControl/DataTemplates when button is pressed? For example, When "next" button is pressed then contents of main window disappear and content of user control comes on the screen of "MainWindow.xaml". Maybe with "" binding, to disable it and enable it?
以及如何在“MainWindow.xaml”中进行“绑定”以在按下按钮时更改UserControl / DataTemplates?例如,当按下“下一步”按钮时,主窗口的内容消失,用户控件的内容出现在“MainWindow.xaml”的屏幕上。也许用“”绑定,禁用它并启用它?
I found some example which function on DataTemplate A Simple MVVM Example. It helped me to implement some things, but I see some discussions over "UserControl" vs. "DataTemplate" and how to do it? So now I am confused :)
我在DataTemplate上发现了一些函数,这是一个简单的MVVM示例。它帮助我实现了一些东西,但我看到一些关于“UserControl”与“DataTemplate”的讨论以及如何做到这一点?所以现在我很困惑:)
1 个解决方案
#1
1
I recently made a WPF application with the MVVM pattern, and I did the following:
我最近用MVVM模式制作了一个WPF应用程序,我做了以下工作:
-
I have one 'Window', the mainwindow, and in this window all UserControls are loaded.
我有一个'Window',主窗口,并在此窗口中加载所有UserControls。
-
Every UserControl has a different Viewmodel, for examples a "GeneralSettingsUserControl" has a GeneralSettingsViewModel for validation and databinding.
每个UserControl都有一个不同的Viewmodel,例如“GeneralSettingsUserControl”有一个GeneralSettingsViewModel用于验证和数据绑定。
-
Every UserControl has its own codebehind where data is bound to its ViewModel
每个UserControl都有自己的代码隐藏,其中数据绑定到其ViewModel
-
The following code I found on the internet (I don't know the url anymore) but for me it did the trick to change de ContentControl in the mainwindow.
我在互联网上找到的以下代码(我不知道网址了)但是对我来说它改变了在主窗口中更改de ContentControl的技巧。
Switcher.cs:
Switcher.cs:
public static mainWindow mainWindow;
public static void switchPage(UserControl p_objNewPage)
{
mainWindow.navigate(p_objNewPage);
}
mainWindow.xaml.cs
mainWindow.xaml.cs
public void navigate(UserControl nextPage)
{
PageContent.Children.Clear();
PageContent.Children.Add(nextPage);
PageContent.LastChildFill = true;
}
PageContent is the name of the Grid where the main content is located. In every UserControl you can call the Switcher.switchPage(new UserControl) to change the content of the window. So when you click a button you can call this method.
PageContent是主要内容所在的Grid的名称。在每个UserControl中,您可以调用Switcher.switchPage(新的UserControl)来更改窗口的内容。因此,当您单击按钮时,您可以调用此方法。
Hope it helps and good luck.
希望它有所帮助,祝你好运。
#1
1
I recently made a WPF application with the MVVM pattern, and I did the following:
我最近用MVVM模式制作了一个WPF应用程序,我做了以下工作:
-
I have one 'Window', the mainwindow, and in this window all UserControls are loaded.
我有一个'Window',主窗口,并在此窗口中加载所有UserControls。
-
Every UserControl has a different Viewmodel, for examples a "GeneralSettingsUserControl" has a GeneralSettingsViewModel for validation and databinding.
每个UserControl都有一个不同的Viewmodel,例如“GeneralSettingsUserControl”有一个GeneralSettingsViewModel用于验证和数据绑定。
-
Every UserControl has its own codebehind where data is bound to its ViewModel
每个UserControl都有自己的代码隐藏,其中数据绑定到其ViewModel
-
The following code I found on the internet (I don't know the url anymore) but for me it did the trick to change de ContentControl in the mainwindow.
我在互联网上找到的以下代码(我不知道网址了)但是对我来说它改变了在主窗口中更改de ContentControl的技巧。
Switcher.cs:
Switcher.cs:
public static mainWindow mainWindow;
public static void switchPage(UserControl p_objNewPage)
{
mainWindow.navigate(p_objNewPage);
}
mainWindow.xaml.cs
mainWindow.xaml.cs
public void navigate(UserControl nextPage)
{
PageContent.Children.Clear();
PageContent.Children.Add(nextPage);
PageContent.LastChildFill = true;
}
PageContent is the name of the Grid where the main content is located. In every UserControl you can call the Switcher.switchPage(new UserControl) to change the content of the window. So when you click a button you can call this method.
PageContent是主要内容所在的Grid的名称。在每个UserControl中,您可以调用Switcher.switchPage(新的UserControl)来更改窗口的内容。因此,当您单击按钮时,您可以调用此方法。
Hope it helps and good luck.
希望它有所帮助,祝你好运。