none
Opening a New Window on a Button click - MVVM WPF RRS feed

  • Question

  • 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?
    Monday, April 6, 2020 11:26 AM

All replies

  • Hi Mash96,
    Based on your description, it is related to WPF. So it is recommended to ask the questions in this forum and you can get more professional answer.
    Thank you for your understanding.
    Best Regards,
    Daniel Zhang



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 7, 2020 1:18 AM