SST
SST

Reputation: 459

How to access one ViewModel's object in another ViewModel in WPF application using MVVM

I am using mvvm pattern in WPF application and not using any database. I stuck in to problem where I have a view UploadView in which user uploads some excel file and its view model named as UploadViewModel in this I read uploaded files data and keeps it in some datatable object which is UploadvViewModals class's object and it is working fine but my problem is that now I have to display uploaded data(datatable object) it on some another view in a some ItemsControl having different Viewmodal. Being a newbie in WPF and windows.I dont know how to proceed. What are the possible ways to do this?

Upvotes: 5

Views: 5930

Answers (3)

Surfbutler
Surfbutler

Reputation: 1528

If you're using MVVM, you should really be accessing the Excel file in your Model class. The ViewModels are normally used for translating model data into a format your Views can display, not for accessing data.

Upvotes: 1

Jordy van Eijk
Jordy van Eijk

Reputation: 2766

You can take a loot at EventAggragation (mediator pattern)

some other tutorial to start

Upvotes: 3

devdigital
devdigital

Reputation: 34349

There are a few ways to do this depending on how loosely coupled the two view models are. If you have a direct reference to the second view model in your upload view model, then you could pass the data when you display this view model/view.

This seems like the most sensible option if a) the data is a requirement of the second view model and b) the upload view model is responsible for the creation of the second view model. In this case, you could pass the data as a dependency in the constructor of the second view model.

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

Upvotes: 1

Related Questions