Search code examples
c#wpfwindowsmvvmdata-binding

How to transfer the binded data in the Main window to another window in WPF?


I have a window that displays a list of Employees. When I select one Employee and click on a button, another window should pop up displaying the details of the selected Employee. But since the new window creates a new instance of my View Model, it does not know which Employee I clicked. I know how to display the details in the same window using a Datagrid or listview but now I am trying to learn navigation in WPF and I really want to know how data is transferred in WPF between different windows. Right now I am using a single View Model for both the windows. Inside the constructor of both the windows, I wrote this.DataContext = new EmployeeViewModel();


Solution

  • There are two patterns with MVVM. View first and VM first. You are using View first and are finding the issues with it.

    VM first is more common and means that the VMs are in charge, and windows and views are just created to wrap the VMs. See my previous answer to see how to open a new Window for a VM in a VM first world.