0

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 ...

I already have several user controls defined. First question is what is better to use, UserControl or DataTemplates to change content of the 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?

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 :)

user4582348
  • 127
  • 3
  • 12
Pukaai
  • 367
  • 1
  • 4
  • 14
  • what kind of UI do you want to create? is there any well known application with similar UI? – Liero Jun 22 '15 at 08:05
  • Is see app on this link: https://www.youtube.com/watch?v=Bk7mlEQI2rk I dont need so fancy app, but the princip is very nice made - att the top is "dockbar" which lead user over the app. It is not very good example, but I dont any other Idea, because I am new in this. And this example probably have some extension added :) – Pukaai Jun 22 '15 at 08:14
  • 1
    Google gives me http://stackoverflow.com/questions/1405739/mvvm-tutorial-from-start-to-finish – Ian Ringrose Jun 22 '15 at 08:21
  • Then read http://stackoverflow.com/help/how-to-ask and ask a detailed question that contains the code you have tried. – Ian Ringrose Jun 22 '15 at 08:22
  • Sorry. Yes my app will configure some device. First I must list all devices connected to listbox and later select devices and program will send data to that selected devices. I have only 4 or 5 different windows with same buttons (next and cancel) and only dockbar is changing its color to actual tab :) Maybe this princip of the program helps anything to anyone. – Pukaai Jun 22 '15 at 08:22
  • For simple scenarious you can use TabControl. Just create as many tabs, as many pages you need. Set UserControl to content of each tab. Otherwise you should have a look at frame navigation. – Liero Jun 22 '15 at 08:22
  • Sorry Ian Ringrose, I have not seen before. I seach by another words in google. Thanks and sorry! – Pukaai Jun 22 '15 at 08:24
  • Thanks Liero. This "tabs" or usercontrol I already have prepared, just I dont know how to implemented, that with "next" button will change:) How to implement "" to Bind every tab? – Pukaai Jun 22 '15 at 08:26

1 Answers1

1

I recently made a WPF application with the MVVM pattern, and I did the following:

  • I have one 'Window', the mainwindow, and in this window all UserControls are loaded.

  • Every UserControl has a different Viewmodel, for examples a "GeneralSettingsUserControl" has a GeneralSettingsViewModel for validation and databinding.

  • Every UserControl has its own codebehind where data is bound to its 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.

Switcher.cs:

public static mainWindow mainWindow;
public static void switchPage(UserControl p_objNewPage)
{
    mainWindow.navigate(p_objNewPage);
}

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.

Hope it helps and good luck.

  • Thanks! I like to made same trick by my app ("... but for me it did the trick to change de ContentControl in the mainwindow."). Your "ContentControl" was datatemplates or usercontrols windows? – Pukaai Jun 22 '15 at 08:18
  • I found the url where I found this piece of code: [link](https://azerdark.wordpress.com/2010/04/23/multi-page-application-in-wpf/) – Jens Delannoy Jun 22 '15 at 08:34
  • Usercontrols. Datatemplates are used to create the layout of a single page, but I'm not sure there is a possibility to add code-behind – Jens Delannoy Jun 22 '15 at 08:35
  • DataTemplates determine how to represent visually a class that already has its own behavior and data. No need for code-behind. But in MVVM that means that if you use DataTemplates for your views, any view-related code must be XAML... And that has its pros and cons. – almulo Jun 22 '15 at 09:53