0

Lets say we have a mainWindow for the application and we want a second window or usercontrole (whichever is best suited) as a settings window. How would we open this window and close it with a mvvmLight approach?

This is currently the code i use for opening a new window(s).

var settingWindow = new SettingsViewWindow();
    settingWindow.Show();

This is currently the code i use for closing a window.

This.Close();

I don't know much about the userControle controle and when to use it. If you know a youtube video or a site i could read about it it would be appritacted. Or just simply drop an explanation.

Below is how i have structured the MenuItem Click Events. For the time beeing this is how i like to struckure my eventhandlers for buttons in general, and place them inside a descreptive regrion. If i know how i would place this bottom part in a spolier. ;)

private void btnNav_Click(object sender, RoutedEventArgs
  {
    if(sender == btnNavSettings)
      {
          OpenSettingsWindow();
      }
      else if(sender == btnNavExitApp)
      {
          ShutDownApplication();
      }
   }

/// <summary>
/// Opens a settings window.
/// Only on settings window can be open at time.
/// </summary>
private void OpenSettingsWindow()
  {
    if(GlobalVariabels.GUI_Variabels.SettingsWindowIsOpen != true)
      {
         var settingWindow = new SettingsViewWindow();
         settingWindow.Show();
      }
  }

/// <summary>
/// Exit the application properly.
/// </summary>
private void ShutDownApplication()
  {
     Application.Current.Shutdown(0);
  }
  • What you are currenly doing violates a lot of MVVM principles. I'll add an explanatory answer when I get an access to a computer. – Kamil Solecki Apr 19 '17 at 14:39

1 Answers1

0

You could for example pass a message between two Components, such as a view model and a view, using the Messenger class:

MVVM Light WPF open new window How to open a new window using MVVM Light Toolkit

Or you could create a dialog service that handles the opening of windows:

Best Pratice to open a New Window in MVVM Light with Parameters

You then inject the view models with such a dialog service:

Separate project for view models to "enforce" MVVM: How do I open dialogs?

There are plenty of links on the subject. Please refer to the ones I have supplied here and Google or Bing if you want more :)

Community
  • 1
  • 1
mm8
  • 163,881
  • 10
  • 57
  • 88