如何处理具有多个 ViewModel 的一个视图并触发不同的命令?
我有一个场景,其中一个视图和视图与多个 ViewModel 绑定。
例如。一个视图显示电话详细信息和 ViewModel,如下所示:
- 电话基本功能 - PhoneViewModel、
- 电话价格详细信息 - PhoneSubscriptionViewModel、
- 电话配件 - PhoneAccessoryViewModel
- 对于常规属性 - PhoneDetailViewModel
我已将视图的常规属性放置到 PhoneViewModel 中。现在情况如下:
默认视图显示与 PhoneViewModel 的 ObservationCollection 绑定的 Phone Basic 功能。我的视图有按钮 - “查看附件”,单击此按钮会出现一个弹出屏幕 - 在我的设计中,我显示/隐藏网格并将其与 PhoneAccessoryViewModel 的 ObservationCollection 绑定。
现在问题开始了 - 附件列表也有按钮“查看详细信息”,单击我必须打开一个弹出屏幕,在这里我还放置了一个网格并可见/隐藏它。我已将“ViewAccessoryDetailCommand”命令绑定到“查看详细信息”按钮。在命令执行时,会触发一个函数并设置弹出屏幕可见的属性。
使用此类编程命令会触发函数调用,但不会引发属性更改,因此我的视图不会显示弹出窗口。
概括: 一种观点--> ViewModel1-->Grid 绑定视图 ViewModel2-->Grid 有按钮和 Onclick 显示与 ViewModel3 绑定的新网格 - 此命令会触发,但属性不会引发。
我认为我的方法存在一些问题,请提出您的建议。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定我完全明白你想问的问题,但我会尝试回答。
我昨天发布了(可能)类似问题的答案。
请参阅
在 WPF 中查询 MVVM 模式?
和
MVVM - 用户控件的理想方式是什么互相交谈
我不确定是否有效,但我希望这会有所帮助。
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.
在 WPF 中,通过将 View 的
DataContext
设置为 ViewModel,View 连接到 ViewModel 。由于每个控件只能有一个
DataContext
,因此 View 只能有一个 ViewModel。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.