从中央数据源在 MVVM 中创建选项卡控件

发布于 2024-10-01 02:08:29 字数 511 浏览 4 评论 0原文

我是 MVVM 新手,我正在尝试遵循该模式实现一个简单的应用程序。

为了简单起见,我将问题分解为最简单的形式。如果我设法让它发挥作用,那么我申请的时候就不会有什么麻烦了。

这个简单的应用程序由一个选项卡控件组成。重要的是两个选项卡都有自己的 ViewModel。然而,他们将从同一来源获取大部分数据。主要问题是让第二个选项卡知道第一个选项卡已对数据源发起更改。

因此,为了简单起见,我们假设模型保存一个整数。该整数最初需要设置为 1。

第一个选项卡包含一个文本块和一个按钮。文本块的文本绑定到数据模型中的整数。按下按钮后,模型中的整数应增加 1。

第二个选项卡仅包含一个文本块,也绑定到数据模型中的整数。挑战是让这个文本块与第一个文本块一起更新,但仍然是它自己的视图模型。

我需要一个中心位置来存储模型的值,并以某种方式让视图模型知道它们已经更新,这样它们的属性就可以更新,视图也因此得到相应的更新。

有人可以尽可能详细地解释这是如何完成的吗?我已经尝试了十亿种不同的方法,但我没有让它发挥作用。

I am new to MVVM, and I am trying to implement a simple application, following the pattern.

For simplicity, I am breaking the problem down to it's simplest form. If I manage to get this to work, I will have little trouble getting the application made.

The simple application consists of a tabcontrol. It is important that both tabs have their own ViewModel. However, they will get most of their data from the same source. The main issue is to get the second tab to know that the first have initiated a change on the datasource.

So, for simplicity, let's just say that the model is holding a single integer. This integer needs initially to be set to 1.

The first tab is holding a textblock and a button. The text of the textblock is bound to the integer in the datamodel. Upon pressing the button, the integer in the moddel should be incremented with 1.

The second tab holds only a textblock, also bound to the integer in the datamodel. The challenge is to get this textblock to update along with the first textblock, but still being it's own viewmodel.

I need somewhere central to store the values of the model, and in some way, let the viewmodels know that they have been updated, so their properties can be updated, and the Views therefore get's updated accordingly.

Can someone explain in as much detail as possible how this is done? I have tried a billion different ways, but I am not getting it to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

你曾走过我的故事 2024-10-08 02:08:29

让我看看我是否正确回答了您的问题:

  • 您有一个数据源(您的模型)。
  • 您有 2 个视图模型。
  • 视图模型 1 更改模型中的数据。
  • 视图模型 2 需要根据模型中的更改进行更新。

如果这一切听起来不错,那么这里有一个解决方案:

让您的模型实现 INotifyPropertyChanged。当整数更改时,引发 PropertyChanged 事件。在视图模型 2 中,侦听模型的 PropertyChanged 事件。当它发生时,引发视图模型 2 的属性更改事件,其 UI 将自动更新。

Let me see if I have your question down right:

  • You have a data source (your model).
  • You have 2 view models.
  • View model 1 changes data in the model.
  • View model 2 needs to update with changes in the model.

If that all sounds right, here's one solution:

Have your model implement INotifyPropertyChanged. When the integer changes, raise the PropertyChanged event. In view model 2, listen for the model's PropertyChanged event. When it occurs, raise view model 2's property changed event, and its UI will get updated automatically.

淡淡離愁欲言轉身 2024-10-08 02:08:29

我不知道你想在哪种情况下这样做。
但我想到的一个解决方案是拥有一个“父”ViewModel,它保存两个选项卡 ViewModel 的实例。

例如

public class ParentViewModel{

   private Tab1ViewModel viewModel1;
   private Tab2ViewModel viewModel2;
}

,然后ParentViewModel可以订阅ViewModel1的INotifyPropertyChanged事件并在ViewModel2上设置值。

I have no idea in which scenario you want to do that.
But a solution that crosses my mind is to have a "parent" ViewModel that holds instances of the two tab ViewModels.

e.g.

public class ParentViewModel{

   private Tab1ViewModel viewModel1;
   private Tab2ViewModel viewModel2;
}

Then the ParentViewModel can subscribe to the INotifyPropertyChanged event of the ViewModel1 and set the value on the ViewModel2.

固执像三岁 2024-10-08 02:08:29

我最近实施了类似的事情。它用于实现一个向导,包括:

  • 7 个视图
  • 8 个视图模型
  • 1 个模型

主 ViewModel 创建模型并通过其构造函数将其传递给所有其他视图模型。

在您的场景中,您可能有一个带有 ViewModel 的 ObservableCollection 的主 ViewModel。这些虚拟机中的每一个都将具有相同的模型实例作为其数据源。

如前所述,在模型上实现 INotifyPropertyChanged 并通过 ViewModel 上的属性将视图直接绑定到模型。我发现这个图非常有用: http://karlshifflett.files.wordpress.com/ 2009/01/wpflobmvvm1.png

I have recently implemented something similar to this. It was for implementing a wizard, consisting of:

  • 7 Views
  • 8 View models
  • 1 Model

The main ViewModel created the Model and passed this on to all the other view models through their constructors.

In your scenario you could have a main ViewModel with an ObservableCollection of ViewModels. Each of these VM's would have the same instance of the model as their data source.

As previously mentioned, implement INotifyPropertyChanged on the model and bind the views directly to the model through a property on the ViewModel. I found this diagram very useful : http://karlshifflett.files.wordpress.com/2009/01/wpflobmvvm1.png

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