How to access one ViewModel in another ViewModel in a WPF application using MVVM

I am using the mvvm template in a WPF application and not using any database. I ran into a problem when I have a UploadView view in which the user uploads some excel file and its view model called UploadViewModel, in this I read the data of the downloaded files and save them in some kind of data object that is an object of the class UploadvViewModals, and it works fine, but my problem is that now I have to display the downloaded data (data object) on some other view in some ItemsControl elements that have different Viewmodal. Being new to WPF and windows.I don't know how to proceed. What are the possible ways to do this?

+4
source share
3 answers

You can take prey in EventAggragation (reseller template)

some other tutorial to start

+3
source

There are several ways to do this, depending on how loose the two viewing models are. If you have a direct link to the second view model in your load view model, you can transfer data when displaying this model / view view.

This seems to be the most reasonable option if: a) the data is a requirement of the second presentation model; b) the loading presentation model is responsible for creating the second presentation model. In this case, you can pass data as a dependency in the constructor of the second presentation model.

Alternative approaches include using an event aggregator as an intermediary to transfer data between two view models, but I would go with the first approach.

+1
source

If you are using MVVM, you really need to access the Excel file in the model class. ViewModels are typically used to translate model data into a format that your views can display, rather than accessing data.

+1
source

Source: https://habr.com/ru/post/1434607/


All Articles