Opening a New Window on a Button click - MVVM WPF

Maneesha Rajaratne 1 Reputation point
2020-04-08T03:45:22.81+00:00

I am new to MVVM architecture, and I would like to keep the standard of MVVM without violating its rules. So I implemented an approach to open a new window on a button click using Services. I don't know if this is the right approach to do so. Here is my code,
Inside the service folder, I have two files. IwindowService and WindowNavService.

interface IWindowService { 
    void CreateWindow();    
}

class WindowNavService : IWindowService {       
      public void CreateWindow(){  
         PrintPreview printPreview = new PrintPreview {              
         DataContext = new PrintPreviewViewModel()                    
         };            
         printPreview.Show(); 
         }    
       }

PrintPreview.xaml is the second window that I want to open.
this is my ViewModel of the main Window.

class PatientRecordDetailsViewModel {       
  WindowNavService windowNav = new WindowNavService();  

           //constructor         
          // some other codes        
          // button command         
  public ICommand SubmitCommand        
  {            
     get => new PatientRecordDetailsCommand(param => this.Submit(), param => CanSubmit());        
  }        
  private void Submit()        
  {            
     IWindowService windowService = windowNav;            
     windowService.CreateWindow();                    
  }        
  private bool CanSubmit()       
  {            
    return true;        
  }
}

I am calling the method of the interface from the button Submit() method. It works fine. But what I need to know is that, if it is violating the standards of MVVM?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,326 Reputation points
    2020-04-08T08:24:03.157+00:00

    Hi, MVVM is not a dogma.

    Model-View-ViewModel (MVVM) is a software design pattern that is structured to separate program logic and user interface controls.

    View is the collection of visible elements, which also receives user input. This includes user interfaces (UI), animations and text. The content of View is not interacted with directly to change what is presented.

    ViewModel is located between the View and Model layers. This is where the controls for interacting with View are housed, while binding is used to connect the UI elements in View to the controls in ViewModel.

    It is ok that the ViewModel creates a new view as a result of the business logic and activate (displays) it. This does not violate the separation of design and logic.

    1 person found this answer helpful.

  2. Alex Li-MSFT 1,096 Reputation points
    2020-04-15T01:56:54.023+00:00

    Welcome to our Microsoft Q&A platform!

    The purpose of MVVM is to separate the UI and business functions. The responsibility of View is to be responsible for how to display data and send commands. The function of ViewModel is to provide data and execute commands. Each department does its job without affecting each other.

    Thanks.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.