如何将两个列表视图的不同选择绑定到 WPF / MVVM 中的相同实例

发布于 2025-01-08 09:40:57 字数 705 浏览 9 评论 0原文

我正在使用 WPF 和 MVVM。 在我的应用程序中,最好从主屏幕中选择一个项目,然后按按钮查看详细信息。应打开一个新窗口,并且可以在列表视图中进行(多个)选择,其中 IsSelected 绑定到 ObservebleCollection 中的项目。

当用户再次按下该按钮(在主屏幕上进行相同的选择)时,会打开另一个具有相同详细信息的窗口。但是,我假设如果做出不同的选择,第一个窗口将同步并显示相同的选择。

我希望两个窗口可以有不同的选择。这可能吗?应该采取什么措施才能使其发挥作用?

更新: 示例:假设我有一辆 4 轮汽车(集合)。两个窗口中的列表视图均显示 4 个轮子(一项为一个轮子)。当我在列表视图中选择滚轮 1 和 2 时,我认为在另一个窗口中也会选择滚轮 1 和 2。但我想在窗口 2 中选择 3 和 4,在窗口 1 中选择轮子 1 和 2)。我不想添加另一个 4 轮子的集合,否则我会拥有一辆 8 轮子的汽车……或 2 辆 4 轮子的汽车,但如果我更改轮子 3 和 4,则只会更改第二辆车。

更新2: 顺便说一句,在我的应用程序中:

  • 我有 0 到 n 个带有列表视图的窗口。
  • 我创建一个包含视图模型的窗口。
  • 每个视图模型都引用“真实”模型。

所选项目的绑定变量位于真实模型内部,我想我需要将其移动到视图模型,但我不知道如何。因为(示例中的轮子)属性位于“真实”模型中,并且这些属性也绑定在同一列表视图(项目)中。 我知道了

I am using WPF and MVVM.
In my app it would be nice to select from a main screen an item and then press a button to see details. A new window should be opened and (multiple) selections can be made in a listview where the IsSelected is bound to the items in a ObservebleCollection.

When the user pressed that button again (for the same selection on the main screen) another window is opened with the same details. However, I assume that if a different selection is made, the first window is synced and will show the same selections.

I would like to have the two windows possible with different selections. Is this possible and what should be done to make it work?

Update:
Example: let's say I would have a car with 4 wheels (Collection). The listviews in both windows show the 4 wheels (one item is one wheel). When I select wheel 1 and 2 in the list view I think in the other window also wheel 1 and 2 will be selected. But I want to select maybe 3 and 4 in window 2 and wheel 1 and 2 in window 1). And I don't want to add another collection of 4 wheels otherwise I would have a car with 8 wheels ... or 2 cars with 4 wheels but if I change wheel 3 and 4, it would change only the second car.

Update 2:
Btw, in my app:

  • I have 0 to n windows with a list view.
  • I create a window including a view model.
  • Each view model is referencing the 'real' model.

The binding variable for the selected items are inside the real model, I guess I need to move this to the view model, but I don't know how. Because the properties (of the wheels in the example) are in the 'real' model and those are binded also in the same list view (items).
I c

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

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

发布评论

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

评论(2

甜心小果奶 2025-01-15 09:40:57

如果只有两个列表,您可以将 IsSelected 绑定到两个单独的属性(即 IsSelectedInList1、IsSelectedInList2)。

但实际上,视图模型应该是 UI 的代码内无视图表示。因此,如果 UI 中有两个(或 N)个列表副本,它们应该具有不同的状态,那么在内存中创建该视图模型的两个(或 N)个副本来表示它是有意义的。

而且我不想添加另一个 4 轮子的集合,否则我会拥有一辆 8 轮子的汽车...或 2 辆 4 轮子的汽车,但如果我更改轮子 3 和 4,则只会更改第二辆车.

我认为在考虑你的模型时更是如此。在您的模型中,您需要一辆特定汽车的一个实例,并且该汽车应该有一组四个轮子。但是,如果您想要两个不同的视图(具有不同的状态),则可以从该模型创建两个视图模型。

If there are only two lists, you could bind IsSelected to two separate properties (ie. IsSelectedInList1, IsSelectedInList2).

But really, a viewmodel is supposed to be an in-code view-less representation of your UI. So, if you have two (or N) copies of the list in your UI which are supposed to have different states, it makes sense to create two (or N) copies of that viewmodel in memory to represent it.

And I don't want to add another collection of 4 wheels otherwise I would have a car with 8 wheels ... or 2 cars with 4 wheels but if I change wheel 3 and 4, it would change only the second car.

I think this is more true when thinking about your model. In your model, you want one instance of a particular car, and that car should have one collection of four wheels. But if you want two different views (with different states), it is OK to create two viewmodels from that model.

活泼老夫 2025-01-15 09:40:57

仅当两个列表都绑定到同一集合(即相同的引用)或绑定到包含相同对象实例的集合时,这两个窗口才会同步。因此,每个视图模型都需要自己独立的集合。

The two windows would only be in sync if both both lists are bound to the same collection (i.e. the same reference), or bound to collections that contain the same object instances. Therefore each view model would need its own independant collection.

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