How To Open A Child Window From View Model In MVVM In WPF

If you are new to MVVM pattern using Prism Library then you can follow my very first article to start the MVVM and add the dlls into the project from the below link,

Now I will show you a demo to show a popup window on the view via the click event of a button from the view in view model.

Note: In this article I am using Visual Studio 2013 and ‘Prism.Unity’ via nugget Packages.

Step 1: Create a project named ‘PrismMVVMTestProject’ of WPF application.

application

Step 2: It’s a better approach to create the three different folders in the project for Model, View and View model respectively.

folders

Step 3: Create pages in all folders

i. Create a View Named ‘TestView.xaml’ as a parent page in the Views folder.

View

ii. Also create a View Named ‘PopUpWindow.xaml’ as a child page in the Views folder.

PopUpWindow

iii. Create a Model Named ‘TestModel.cs’ in the Models folder.

TestModel

Note: We don’t not need Model in this article, I have just created it for the best practices.

iv. Create a ViewModel Named ‘TestViewModel.cs’ in the ViewModels folder.

ViewModels

Step 4: Add a label on the child window page named ‘PopupWindow.xaml’

  1. <Label Content="This is Pop Up window" HorizontalAlignment="Left" VerticalAlignment="Top"  
  2.   
  3. Margin="67,113,0,0"/>  

label

Step 5:

a. Add the below namespaces on the TestViewModel page,

  • ‘Prism.MVVM’ To inherit the class named ‘Bindable Base’.
  • PrismMVVMTestProject.Views’ To accessing child window in this page.
  • System.Windows.Input To add ICommand.
  • Prism.Commands To Use DelegateCommand.

b. Create a command named ‘ShowCommand’

c. Attach that command to the method named ‘ShowMethod’ which will act like event where will add the show the child window from the view model.

d. Create a object of child window and show it,

  1. using PrismMVVMTestProject.Views;  
  2. using Prism.Mvvm;  
  3. using System.Windows.Input;  
  4. using Prism.Commands;  
  5. using System.Windows.Data;  
  6. namespace PrismMVVMTestProject.ViewModels  
  7. {  
  8.     class TestViewModel: BindableBase   
  9.     {  
  10.         public ICommand ShowCommand   
  11.         {  
  12.             get;  
  13.             private set;  
  14.         }  
  15.         public TestViewModel() {  
  16.             ShowCommand = new DelegateCommand(ShowMethod);  
  17.         }  
  18.         private void ShowMethod() {  
  19.             PopUpWindow objPopupwindow = new PopUpWindow();  
  20.             objPopupwindow.Show();  
  21.         }  
  22.     }  
  23. }  

code

Step 6:

· Add a button to show the child window with ShowCommand property and bind it that command which we have created in view model.

  1. <Button Content="Show Popup" Command="{Binding ShowCommand}" HorizontalAlignment="Left"  
  2.   
  3. VerticalAlignment="Top" Width="75" Margin="31,21,0,0"/>  

code

Step 7: AddPrismMVVMTestProject.ViewModels’ namespace and bind Data Context of TestView Page to the ViewModel named ‘TestViewModel’.

  1. using System.Windows;  
  2. using PrismMVVMTestProject.ViewModels;  
  3. namespace PrismMVVMTestProject.Views  
  4. {  
  5.     /// <summary>  
  6.     /// Interaction logic for TestView.xaml  
  7.     /// </summary>  
  8.     public partial class TestView: Window   
  9.     {  
  10.         public TestView()  
  11.         {  
  12.             InitializeComponent();  
  13.             this.DataContext = new TestViewModel();  
  14.         }  
  15.     }  
  16. }  

code

Step 8: Change the ‘StartupUri’ from default page ‘MainWindow’ to ‘TestView’ page,

TestView

Run the page and See the Output:

Output

After click on the ‘Show PopUp’ button, it will show the child window.

PopUp

Read more articles on WPF:

Up Next
    Ebook Download
    View all
    Learn
    View all