views:

229

answers:

2

Hi All,

I have senario in which one view and view has binding with multiple ViewModel.

Eg. One View displaying Phone Detail and ViewModel as per bellow:

  • Phone basic features- PhoneViewModel,
  • Phone Price Detail- PhoneSubscriptionViewModel,
  • Phone Accessories- PhoneAccessoryViewModel
  • For general properties- PhoneDetailViewModel

I have placed View's general properties to PhoneViewModel.Now senario is like this:

By default View displays Phone Basic feaures which is bind with ObservationCollection of PhoneViewModel. My view have button - 'View Accessories', onclick of this button one popup screen- in my design I have display/hide Grid and bind it with ObservationCollection of PhoneAccessoryViewModel.

Now problem begins- Accessory List also have button 'View Detail' onclick I have to open one popup screen, here also I had placed one Grid and Visible/Hide it. I have bind 'ViewAccessoryDetailCommand' command to 'View Detail' button. And on command execution one function fires and set property which Visible the Popup screen.

Using such programming command fires, function calls but the property change not raises and so my view does not display popup.

Summary: One View--> ViewModel1-->Grid Bind view ViewModel2 -->Grid Have Button and Onclick display new Grid which binded with ViewModel3-this Command fires but property not raises.

I think there is some problem in my methodology, Please, give your suggetions.

A: 

In WPF a View is connected to a ViewModel by setting the DataContext of the View to the ViewModel.

Since each control can only have one DataContext a View can only have one ViewModel.

Cameron MacFarland
Thank you for replay, but senario in which we need to display one to may related data. Suppose we have category-product relation. We are displaying category and onclick of category displaying Products, which have individual ViewModel. So, in this senario we need multiple ViewModel.
Naresh
In that scenario I'd have multiple views too. One view displays all the categories, and when you select a category it shows a view as a separate usercontrol to display the product.
Cameron MacFarland
A: 

I'm not sure I completely follow what you are trying to ask, but I'll have a go at answering. I posted an answer to (possibly) a similar question yesterday.

See

http://stackoverflow.com/questions/2430376/query-on-mvvm-pattern-in-wpf/2430488#2430488

and

http://stackoverflow.com/questions/1798600/mvvm-what-is-the-ideal-way-for-usercontrols-to-talk-to-each-other

I'm not sure if it does, but I hope this helps.

Andrew Bienert