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();
Asked
Active
Viewed 56 times
0

Harisankar V
- 11
- 3
-
Why not just pass the model to the new window? – Terry Tyson Dec 09 '20 at 22:14
1 Answers
1
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.

GazTheDestroyer
- 20,722
- 9
- 70
- 103
-
Thank you for your answer. I didn't know that such a classification existed. Can you please suggest where I can learn more about VM First Approach. I searched a bit but didn't get any detailed explanation. I am new to WPF. – Harisankar V Dec 09 '20 at 17:04
-
The tutorial by [Josh Smith](https://learn.microsoft.com/en-us/archive/msdn-magazine/2009/february/patterns-wpf-apps-with-the-model-view-viewmodel-design-pattern) is a decent introduction, although it doesn't mention VM first explicitly, that's the pattern it uses. See the "Applying a View to a ViewModel" section for details of how the views are spun up. – GazTheDestroyer Dec 10 '20 at 09:25