我可以在 canExecute 谓词中再添加一个条件吗?

发布于 2024-10-09 10:54:41 字数 185 浏览 0 评论 0原文

我在 WPF MVVM 应用程序中使用 Josh Smith 的 RelayCommand。

我有一个视图模型。在其中我引用了另一个 ViewModel。

子 ViewModel 有一个 ICommand 类型的属性。

在我的父 ViewModel 中,我可以向“CanExecute”谓词再添加一个条件吗?

I am using Josh Smith's RelayCommand in my WPF MVVM application.

I have a ViewModel. Inside that I have a reference to another ViewModel.

The child ViewModel has a property of type ICommand in it.

In my parent ViewModel, can I add one more condition to the "CanExecute" predicate?

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

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

发布评论

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

评论(1

从来不烧饼 2024-10-16 10:54:41

您至少有两个选择:

  1. 您可以从父视图模型公开一个新的 ICommand,该视图模型在新命令的 CanExecute 实现中使用子视图模型。
  2. 您可以在子视图中添加对父视图模型的引用,并在子视图的 CanExecute 实现中调用父视图模型的方法。

第二个选项在孩子身上可能看起来像这样:

SampleCommand = new RelayCommand(..., param => ChildSampleCanExecute() && parentViewModel.ParentSampleCanExecute());

You have at least two choices:

  1. You can expose a new ICommand from the parent viewmodel that uses the child viewmodel in its implementation of CanExecute of the new command.
  2. You can add a reference to the parent viewmodel in the child and call a method on the parent in the child's implementation of CanExecute.

The second option might look something like this in the child:

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