binding properties between viewmodels?

Apr 8, 2010 at 7:31 AM

Hi 

how would you bind notify-able properties between viewmodels? 

say i have 2 viewmodels that exposes properties.

i would like to do a binding operation between both viewmodels and would like put the binding code elsewhere like some ViewModelsBinding.cs file where i specify all bindings across viewmodels

is it good practice to do that?

Apr 8, 2010 at 11:33 AM

I would use messaging to communicate between view models.  See http://www.galasoft.ch/blogs-all/resources/MIX10/Mix10.Messaging.zip for an example.

If that doesn't answer your question, please give a more detailed description of what you're trying to accomplish.

Apr 8, 2010 at 2:53 PM
Edited Apr 8, 2010 at 2:53 PM

HI,

yeah, i know there is the messenger. But i would like the twoway binding/capability. so whenever one of the viewmodels modifies the property, the other would be updated as well.

and i also would like know if it is possible to have a .cs file that specifies all the bindings between viewmodels instead of putting the binding code in the viewmodel.. just want to know if it is good practice?

thanks

Apr 8, 2010 at 3:08 PM

After reading these details, my initial thought is wondering why these are separate views.

I don't know that I've seen any guidance on MVVM where you have disparate views data bound to each other's values.

You could also try creating a global view model and using the ViewModelLocator to set the data context of individual elements that need to bind to values from that view model.

May 17, 2010 at 7:20 PM

I can think of exactly the reason. Right now I have a three-pane UI design where the first pane is bound to the "shell" viewmodel, the second pane selects that subset of data to edit, and the third pane lets you actually edit the data. The way I have it wired currently, the 2nd pane has TreeView containing TreeItems with Tags that point to the XAML page to load, and the 3rd panel is a Frame that dynamically loads and presents the Ui to the user.

I have the different ViewModels working separately, but now I need to be able to pass the selected item in the TreeView in the first pane to the ObjectSource property in the ViewModel for the 3rd pane (or setting selected sets a property in the "shell window" ViewModel, which the 3rd pane ViewModel could bind to.

Ideas?

Coordinator
May 17, 2010 at 10:48 PM

Hi,

I developed the Messenger exactly for this kind of scenarios. I would really recommend you take a good look at it, it is made to enable loosely coupled communication between viewmodels (and other objects as well).

Greetings,

Laurent