I use MVVM Light and used a packaged messaging system to exchange data between view models, however I ran into some dilemma! Basically, when a user clicks on a customer record, the corresponding view is opened and an instance of CustomerViewModel is created with it. At this point, the CustomerViewModel requires that the selected customer identifier from the previous view model (ViewAllCustomersViewModel) so that it can retrieve the selected customer information that the view is bound to (still?). Therefore, initially, my thought also sent this identifier in a message from ViewAllCustomersViewModel (where the client is selected for viewing) to CustomerViewModel ... HOWEVER, CustomerViewModel is not created to be able to receive a message before loading the view (at that moment the message was already sent)!
So what would be the best way to solve this problem? So far, I have been considering a CustomerViewModel sending a request to the ViewAllCustomersViewModel after creating it (basically saying "I am ready to accept the message"), and then the ViewAllCustomersViewModel sends the identifier back to the CustomerViewModel ... but is this a necessary approach to solve this problem? It seems a little ugly to me!
Otherwise, I thought, is there any other way of communicating that can explain the problem I am having? But then, is not all the meaning of the messaging system ... to be able to communicate between viewing models? OR can I get an instance of the view model to start at startup? If so, how will this affect ViewModelLocator?
I hope that I have clearly stated the problem, I used the fictitious names of the models for the purpose of explanation ... and feel free to edit or suggest any additional information that you would like to add!
c # wpf viewmodel mvvm mvvm-light
Sam
source share