Best way to pass Data object in nested View/ViewModels

Oct 22, 2010 at 10:10 PM

Hi,

I have interesting question. What is the best way to pass around a Data object of the View/ViewModel instance to all the child View/ViewModels?

Setup:

 I have a MainPage that have a TabControl, and some structure to find/open accounts. Now, when account is opened, Im creating a new TabItem, with Statefullcontrol that is pointed to the AccountId ("Views/Content/AccountDetails/{AccountId}"). On that ViewModel i get all account related data. Now, what i want to do, is to create a child Views that will be bound to some portion of the Account data (Billing information, contact, etc) (as the there is different account types that should be rendered differently). How is the best way to do so? Is there a way to access a parent ViewModel for the child container?

As a option i see a delay child view initialization with NavigateBehaviour and a parameters binding to the account data (delayed, as the on rendering, there is not account data, and i load it on the fly), could do some Channel Messaging or event Services :)

Oct 23, 2010 at 11:21 AM

Thanks everybody for trying ^^

I've found out that if you create a child View only, and not View/ViewModel, the binding is applied to View also.

Coordinator
Oct 23, 2010 at 12:49 PM

SonicFlare, you are right - the data-context flows down from parent to view. On the other hand, in similar situations when using Child-Views with ViewModels, I've tended to pass the domain objects using navigation parameters and it works quite well. 

Also, you might find some of the discussions (and code) in this post useful http://nroute.codeplex.com/Thread/View.aspx?ThreadId=225598

Cheers,
Rishi