Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Opening a New Window on a Button click - MVVM WPF

$
0
0
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?

Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>