views:

35

answers:

3

Not sure I'm designing this correctly (guidence is appreciated!), but I have a search view which is called from many places. Search screen has has it's own ViewModel. When a user selects something from the search screen (after searching :), I need to send the selection to whatever view requested the search to take place (search screen is a pop-up).

I have a view locator that pops up the screen screen when I send it a message. The message is sent from a ViewModel (MVVM Light) to the view locator.

Do I register a call back? Broadcast a message? Set a Property? Dynamically Bind the "Select" button on the search screen back to the view model that initiated the call?

mmmm so many questions, so little time...

Regards, Richard

A: 

I recommend that you pass the reference to the interface element making the call forward to your search. All of this is taking place at the view-model layer as far as I can tell.

The other option is to set a flag in the search result or search object that registers which interface made the call. I'm not sure how you would go about making a callback in this case.

mvrak
+1  A: 

I am not sure if MVVM Light has anything equivalent to an EventAggregator, but this is the best way I can think of for you to broadcast this event.

Another option would be to use the Reactive Extensions (Rx) and make the receiver subscribe to the event using a filter before you open the search view, then pass the filter to the search view so that the actual operation is called using that filter, so even when the search view model requested the search is the other view model that will receive the event.

Murven
I would love to see an example or some thought on this. I have been struggling with generally how to respond to view events in the ViewModel layer. I just found the reactive framework, which I find very interesting, but I'm just skimming the surface at the moment.I marked your response as an answer, as I feel it is the right design. I may not be able to implement it however. (PRISM might be the answer here - but I've heard that it is pretty heavy layer to incorporate.)
codputer
I don't mean to plug my own project too much, but ReactiveXaml helps you do this solution quite a bit by providing a lot of the "glue code" required to do this stuff - check it out at http://bit.ly/cqQmtW
Paul Betts
+1  A: 

I had a similar issue and here is how I did it.

I had a difference ViewModel for my Pop Up window and the view model took a parameter of the object you want to return e.g. You have you MainViewModel and a property Customer. In the PopupViewModel constructor I passed the Customer from MainViewModel. When I did my search and found the Customer you are looking for, assign that customer to the Customer reference from MainViewModel. Assuming you have implemented INotifyPropertyChange interface, it will show up in your main screen as soon as you select a customer. Let me know if you understand or I will post you an example.

Hope this helps.

Jag
Thanks for your comment - it made me take somemore time to reflect on what I was doing, and how I might implement it a bit differently.
codputer