MVVM/命令和控制模板

发布于 2024-08-16 03:20:52 字数 702 浏览 1 评论 0原文

我目前正在使用 MVVM/Commands 和 ControlTemplates。 我想做的事情很简单。我想要一个例如人员的列表,用户可以在其中触发每个人的操作。 但是事件/命令处理程序不需要在人员对象上执行,例如因为我想用按钮将其从列表中删除。

所以到目前为止我所做的就是创建一个 VM,它公开 PersonVM 的列表,并且每个 VM 都保存我的 person 对象。然后,我使用控件模板创建了一个 CustomControl,其中包含一个显示我的人员的 ItemsControl。

所有数据都显示得很好,没有任何问题。

然后我尝试添加允许用户从列表中删除人员的部分。 所以我在数据模板中添加了一个按钮。仍然不是问题。

下一步是公开由按钮触发的命令。

第一种方法是使用 Josh Smith 的 RelayCommand。因此,我将其中之一添加到 PersonViewModel 并将其绑定到按钮。 它有效,但问题是我需要引发父虚拟机必须处理的事件(在执行命令后)。恕我直言,这不太好,因为随着越来越多的命令,代码对我来说似乎很混乱。

因此,我重新设计了所有内容以使用 RoutedCommand,并尝试在控件层次结构中的更高级别上添加 CommandBinding。但这是不可能的,因为 ControlTemplate 不允许我在视图模型上调用方法。

所以我想知道解决这个问题的正确方法是什么?不知怎的,我相信我走错了路,但我不知道错在哪里。

蒂亚 马丁

I'm currently playing around with a MVVM/Commands and ControlTemplates.
What I'm trying to do is quite simple. I want to have a list of e.g. Persons, where the user can trigger an action on each of them.
But the event/commandhandler needs to be executed not on the person object e.g. because I want remove it from the list with a button.

So what I did so far was to create a VM that exposes a list of PersonVM's and each of the holds my person object. Then I created a CustomControl with a controltemplate that includes an ItemsControl that displayes my persons.

All the data is displayed nicely, and no problem at all.

Then I tried to add the part that allows the user to remove a person from the list.
So I added a button to the datatemplate. Still not a problem.

Next step was to expose a command that is triggered by the button.

The first approach was to use Josh Smiths RelayCommand. So I added one of those to the PersonViewModel and bound it to the button.
It works, but the problem is that I need to raise an event (after the command is executed) that the parent vm has to handle. Imho that is not nice, as with more and more commands the code seems messy to me.

So I reworked everything to use RoutedCommand, and tried to add a CommandBinding on a higher level in the control hierarchy. But this is not possible as the ControlTemplate doesn't allow me to call a method on the viewmodel.

So I'm wondering what is the correct approach for that problem? Somehow I believe that I'm on the wrong track, but I don't see where.

tia
Martin

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

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

发布评论

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

评论(2

愚人国度 2024-08-23 03:20:52

我所做的事情(如果可能有点混乱)是使用relativesource绑定来遍历DataContext(或命令绑定)备份逻辑树,a la:

Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.MyCommandOnTheViewModel}"

Something I've done that works, if potentially a bit messy, is to use RelativeSource bindings to walk the DataContext (or command binding) back up the logical tree, a la:

Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.MyCommandOnTheViewModel}"
不回头走下去 2024-08-23 03:20:52

当我使用 MVVM 模式传递事件时,我发现 Prism 的事件聚合器 非常有用。它允许您处理松散耦合的通信。

因此,一般来说,如果我想在没有显式关系的视图模型之间传递诸如删除之类的事件,我将订阅一个视图模型来查找特定的事件聚合,然后从适当的位置触发该事件。

我确信这种方法有多种变体,所以可能值得一看?

When I pass events around using the MVVM pattern, I found Prism’s Event Aggregator extremely useful. It allows you to handle loosely coupled communication.

So generally speaking if I want to pass events like a delete across View Models that do not have an explicit relationship, I will subscribe the one View Model to look for the a specific event aggregation, and then trigger that from the appropriate places.

I am sure there are several variants of this approach, so it may be worth a look?

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