开发者

Silverlight Command Binding a button dynamically? MVVM

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 s开发者_StackOverflow中文版creen back to the view model that initiated the call?

mmmm so many questions, so little time...

Regards, Richard


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜