Question regarding commanding and view model locator

Jun 20, 2011 at 4:55 PM

Tony,

I've been evaluating a number of frameworks and just as I was about to commit to MVVM Light I stumbled upon Simple MVVM. While I have not dug into the framework yet (in the process right now) I had a question regarding Simple MVVM as it my address one of my specific project needs. Within my silverlight application I have a number of custom user controls which all inheritted their base data context from their parent view (See pseudo diagram below)

View
   - usercontrol A (listbox)
       -  usercontrol B (listbox templated Item)
                 - button within user control B

Where I have continually run into problems is in having events call commands from the view model. While I have used the blend interactivity.dll to handle the EventToCommand I find that I am forced to set the data context of the button to my view model within the xaml. Doing this always instantiats a new VM which would blow out any parameters etc that I may be passing to the VM via  INPC.  I have a specific need to return a collection to listbox, which the user can then click to select a row. This selectionChange event captured an id which would be passed to the vm via INPC. This id would then be used as a parameter that would be included in the command call back to the service to retrieve additional data.

MVVM Light provides a means to allow objects within a user control of retaining the data context of the parent object(View) vs. limiting it to just the context of the user control.  While I have created binding helper classes to overcome this issue I have found my approach to be a bit on the hacky side and not something that I feel comfortable placing in a production environment.

Can Simple MVVM help me overcome these obstacles or does Simple MVVM provide more elegant asolution to this type of interaction/process? If so, are there any existing samples or documents out that I can reference to build my own proof of concept app?

Thank you

randy

Jun 21, 2011 at 4:35 PM

Tony,

In reviewing the samples it appears that the ViewModelLocator does in fact address this need.  I really like what I have seen thus far and look forward to working/learning more about the SimpleMVVM framework.

Best Regards,


Randy

Coordinator
Jun 29, 2011 at 8:18 PM

@Randy: Thanks for trying out the Simple MVVM Toolkit -- I hope you find it straightforward, easy to use, and including the features you need to get the job done.  You are correct that the ViewModelLocator class can pass parameters to the constructor of the ViewModel.  The View sets the DataContext of the page to a property on the locator, which creates the ViewModel on the fly and can even inject either a real or mock service agent using MEF.

In terms of interaction between various user controls, I would advocate using the Message Bus to send messages between ViewModels.  The base ViewModel class has two methods to help here: RegisterToReceiveMessage and SendMessage.  The payload is a NotificationEventArgs class that has generic versions for passing data structures.  The communication can be one-way or two-way.

There is a MessageBus sample that comes with the toolkit and shows you how to use these methods.

Please let me now how if this addresses your situation.

Regards,

Tony