如何通过绑定在 ViewModel 中设置值?
这是一个奇怪的问题——如果我的想法是错误的,请告诉我。我正在使用基础设施停靠管理器,它也可以管理选项卡。因此,我可以创建一个 TabGroupPane
,然后添加多个 ContentPanes
,每个都有自己的选项卡。
在每个内容窗格中,我设置了视图模型:
<ContentPane>
<viewmodels:MyViewModelForTab1 />
</ContentPane>
所以这就是问题 - 在使用中介模式进行通信时,我的视图模型不知道它们是否位于可见选项卡上,因此即使隐藏,它们也始终在工作。 TabGroupPane
确实有一个 SelectedTab
属性,每个 ContentPane
都有一个 IsActive
属性。
所以问题是如何在 ViewModel 中设置该信息?让我的虚拟机成为依赖对象似乎是一个坏主意,因为我已经实现了 INotifyPropertyChanged。在我的虚拟机中使用 CLR prop 也不起作用,因为您无法绑定到它。
如何让我的虚拟机知道它是否是活动选项卡的数据上下文?
谢谢!
Kind of an odd question- if I'm thinking of this the wrong way please let me know. I am using an infragistics dock manager, which manages tabs as well. So I can create a TabGroupPane
, and then add multiple ContentPanes
, each of which has its own tab.
In each content pane, I set my viewmodel:
<ContentPane>
<viewmodels:MyViewModelForTab1 />
</ContentPane>
So here's the problem- while using a mediator pattern for communication, my viewmodels have no idea if they are on the visible tab or not, so they are always working even if hidden. The TabGroupPane
does have a SelectedTab
property, as well as each ContentPane
having an IsActive
property.
So the question is how do I set that information in my ViewModel? Making my VM a dependency object seems like a bad idea since I already implement INotifyPropertyChanged
. Using a CLR prop in my VM also doesnt work, since you cannot bind to it.
How can I get my VM to know if it is the datacontext of an active tab?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将在 ViewModel 上放置一个
IsSelected
属性,并将其绑定到 TabItem 的IsSelected
依赖属性。这应该允许您在更新时进行连接并执行您需要的任何操作。这里不需要中介者模式,因为您是从视图到视图模型进行通信。
确保您的 ViewModel 绑定到视图的 DataContext 属性(具体来说,选项卡的 DataContext 是 ViewModel)。按照你现在的方式,你的 ViewModel 是元素的内容,而不是绑定到 DataContext,因为它应该是:
或者类似的东西......
I would put an
IsSelected
property on my ViewModel and bind it to the TabItem'sIsSelected
dependency property.This should allow you to hook into when it is updated and perform whatever you need. You don't need a mediator pattern here since you are communicatining from the View to the ViewModel.
Make sure your ViewModel is bound to the DataContext property of the view (specifically, that the Tab's DataContext is the ViewModel). The way you have it now, your ViewModel is the content of the element, not bound to the DataContext, as it should be:
Or something like that...
我不知道 Infragistics 模型,所以如果这不合适,我很抱歉,但以下是我如何使用常规项目控件(选项卡控件、列表框等)实现此目的。
创建一个容器视图模型类,其中包含可观察的项目集合并公开
SelectedItem
属性。使容器类成为项目控件的数据上下文。将项目控件的SelectedItem
属性绑定到容器类的属性。将项目对象与容器的
PropertyChanged
事件挂钩。因此,现在当 UI 中的选定项发生更改时,容器视图模型会通知所有SelectedItem
已更改的项。每个项目对象的事件处理程序都可以自行确定它现在是否是选定的项目。因此,项目对象不知道 UI 的任何实现细节 - 您可以在 UI 之外对您的类进行单元测试,并且逻辑仍然可以正常工作。
I don't know the Infragistics model, so my apologies if this is inapposite, but here's how I implement this with regular items controls - tab control, list box, whatever.
Create a container view model class that includes an observable collection of items and exposes a
SelectedItem
property. Make the container class the data context of the items control. Bind the items control'sSelectedItem
property to the container class's.Hook the item objects up to the
PropertyChanged
event of the container. So now when the selected item in the UI changes, the container view model notifies all of the items thatSelectedItem
has changed. Each item object's event handler can determine for itself whether or not it's now the selected item.The item objects thus don't know any implementation details of the UI - you can unit test your classes outside of a UI and the logic will still work correctly.