Anja Simonsen

Anja Simonsen

  • NA
  • 41
  • 15.4k

Wpf Mvvm Multiple (2) views and the same instance of a viewmodel

Apr 21 2021 6:54 PM
My solution is created in projects:
AnsiBug.MainWindow = the UI
In the above there is an App.xaml

Dal.Account.DalCategory
Model.Account.CategoryModel
ViewModel.Account.CategoryViewModel

In the next project (which is usercontrols) I have 2 usercontrols/views
Views.Account.AccountObsoleteView.xaml
Views.Account.AccountObsoleteViewCategoryNew.xaml

Now I want to make one single instance of the CategoryModel in the 2 usercontrols.

I have read I should write following in the app.xaml
  1. <local:MainWindow x:Key="ViewModel.Account.CategoryViewModel" />  
An in the two usercontrols.
  1. DataContext="{StaticResource ViewModel.Account.CategoryViewModel}"  
There is no error before running the code, but when running it fails in the first usercontrol Views.Account.AccountObsoleteView.xaml

The InnerException sais Ressourcen ViewModel.Account.CategoryViewModel was not found.

So it can't find the CategoryViewModel from the ViewModel project.

Can you see why (and telling me the error I make)?

And if you are asking why - is because, that I want to get data from usercontrol 2 to 1 

Best regards
Simsen :-)