-1

I want to transfer a token from viewmodel to view in wpf/xaml/mvvmlight. This token is a public property in the viewmodel and in the view. In the viewmodel-ctor the token gets created (simple guid). This exact token should be transported to the token-property of the view. Here at SO I read this binding between the viewmodel and view property can be done via xaml binding. But I don‘t know how to do it.

The background is the messenger of mvvmlight. I want the view to be able to send messages only to his specific viewmodel and therefore I have to use a common unique token for this view/viewmodel pair.

Or, if you have an other idea, I‘m open to it, so let me know. Thanks guys and girls!

MikeRoss
  • 51
  • 1
  • 9
  • Implement INotifyPropertyChanged in your viewmodel and call NotifyPropertyChange delegate from your property. After this, you need to use property in your view. For simple example, [have a look here](https://www.codeproject.com/Articles/41817/Implementing-INotifyPropertyChanged) – Akash KC Nov 04 '17 at 23:04
  • Thanks, so the keyword here is DependencyProperty? I know the inpc, but thought it would only be for updating the xaml view, nit the code-behind view-class. I‘ ll that tomorrow. Thanks so far – MikeRoss Nov 04 '17 at 23:17
  • I can't speak for DependencyProperty as I've never used it in viewmodel so far. I've used INotifyPropertyChanged for property binding between view and ViewModel. – Akash KC Nov 04 '17 at 23:22
  • I used inpc for updating the xaml ui. But never used it for setting a view class property value to the viewmodel property value without displaying this value on the xaml ui. But i‘ll try your link. Sadly, here‘s after midnight right now ;) – MikeRoss Nov 04 '17 at 23:26
  • Unfortunately, it is not clear at all what you are asking. The word "token" could mean any variety of things, as can "transported", and your question doesn't describe in the slightest what the view will do with that "token" once it has it. Or even what the view _is_. For XAML binding, the target property (i.e. in the view) will have to be a dependency property. But it might already be, depending on how you've set up the view and its data context and where you actually want the "token" to be bound to and what you want that target to do with it. Please fix your question so it's not so vague. – Peter Duniho Nov 04 '17 at 23:26
  • Thanks Peter. „Token“ is a string property of the mvvm light messenger. It should be unique to do private decoupled communication between objects. Here, i want to send and receive messages between a view and its viewmodel. So both must know the guid (token). An other SO thread said, creating a property „token“ in the vm and in the view, set it in the vm to a value and bind theses properties via xaml together should do the trick. The last step (binding via xaml) is not clear to me, so i asked. The token should not show up in the ui but must be present in the view. – MikeRoss Nov 04 '17 at 23:31

1 Answers1

0

I think you just want the capture the message in the code-behind of the view?

So in your VM:

Messenger.Default.Send<MessageObjType>(TheMessageObject, "IdentifyingToken");

In you view code-behind, probably in the constructor:

Messenger.Default.Register<MessageObjType>(this, "IdentifyingToken", 
(TheMessageObject) => 
  {
    // Do something with TheMessageObject
  });

Depending what you are doing though it probably makes more sense just to use binding and RaisePropertyChanged instead of the Messaging system for this...

Ryan
  • 127
  • 1
  • 12
  • No sorry - that's not what I was asking for. In your case/code, you are refering to an IdentifyingToken, what the viewmodel and the view know. But in my case, this is a dynamic token (like a guid), that the viewmodel creates and the view should get to know to send a private, tokenized message to its viewmodel. In this SO post [link](https://stackoverflow.com/questions/20922165/send-a-message-from-view-to-viewmodel-what-is-an-appropriate-token) "Possible Solutions Option 1" he describes his way, but I don't understand it and I'm not allowed to comment there. – MikeRoss Nov 06 '17 at 16:20