MVVM light - how to access property in another view model

I am using mvvm light to create a Silverlight application. Is there a piece of code that shows how to access the model view or command from another view model or user control code?

I think it's simple, but somehow I missed something.

Ueli

+15
silverlight mvvm mvvm-light
Apr 23 '10 at 14:27
source share
3 answers

You can use Messenger to do this: Send the user to UserViewModel:

Messenger.Send<User>(userInstance); 

just send the user to everyone who is interested.

And register the recipient in CardViewModel:

 Messenger.Register<User>(this, delegate(User curUser){_curUser = curUser;}); 

or you can also send a request from your CardViewModel in order to shout out the user:

 Messenger.Send<String, UserViewModel>("Gimme user"); 

And respond to this in the UserViewModel:

 Messenger.Register<String>(this, delegate(String msg) { if(msg == "Gimme user") Messenger.Send<User>(userInstance); }); 

(It is better to use an enumeration rather than a string in a real scenario :))

Perkhubs you can even answer directly, but I can not check it at the moment.

Just check it out: mvvm light messenger

+34
Apr 23 '10 at 16:40
source share

Another way is to use the RaisePropertyChanged overload, which also passes this change. You would do this:

 RaisePropertyChanged(() => MyProperty, oldValue, newValue, true); 

Then in the subscriber:

 Messenger.Default.Register<PropertyChangedMessage<T>>(this, Handler); 

where T is the type MyProperty.

Cheers Laurent

+4
May 31 '13 at 19:51
source share

Another way to look at the problem is to get a service that returns a "currently logged in user".

In any case, the responsibility for processing who is logged in is more dependent on the service, and ViewModels remain simple.

ViewModels must exist for presentation only. But as good citizens, they can also help other ViewModels, such as Laurent and CodeWeasel.

0
Jan 22 '14 at 12:50
source share



All Articles