如何通过绑定在 ViewModel 中设置值?

发布于 2024-08-23 08:54:27 字数 635 浏览 1 评论 0原文

这是一个奇怪的问题——如果我的想法是错误的,请告诉我。我正在使用基础设施停靠管理器,它也可以管理选项卡。因此,我可以创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

給妳壹絲溫柔 2024-08-30 08:54:27

我将在 ViewModel 上放置一个 IsSelected 属性,并将其绑定到 TabItem 的 IsSelected 依赖属性。

这应该允许您在更新时进行连接并执行您需要的任何操作。这里不需要中介者模式,因为您是从视图到视图模型进行通信。

确保您的 ViewModel 绑定到视图的 DataContext 属性(具体来说,选项卡的 DataContext 是 ViewModel)。按照你现在的方式,你的 ViewModel 是元素的内容,而不是绑定到 DataContext,因为它应该是:

<Tab.Resources>
    <viewmodels:MyViewModelForTab1 x:Key="Tab1ViewModel" />
</Tab.Resources>

<ContentPane DataContext="{StaticResource Tab1ViewModel}" />

或者类似的东西......

I would put an IsSelected property on my ViewModel and bind it to the TabItem's IsSelected 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:

<Tab.Resources>
    <viewmodels:MyViewModelForTab1 x:Key="Tab1ViewModel" />
</Tab.Resources>

<ContentPane DataContext="{StaticResource Tab1ViewModel}" />

Or something like that...

心作怪 2024-08-30 08:54:27

我不知道 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's SelectedItem 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 that SelectedItem 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文