How to handle One View with multiple ViewModel and fire different Commands?
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 开发者_如何学Cview 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.
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
Query on MVVM pattern in WPF?
and
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.
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.
精彩评论