将 CanExecute 设置为数据上下文的方法
我可能会以错误的方式处理这件事。我试图在视图后面放置尽可能少的代码,因此我希望在我的视图模型中处理命令绑定的 CanExecute
和 Executed
事件,即我的数据上下文。
我确信我错过了一些非常简单的东西,但我不知道该怎么做。
命令绑定的 XAML:
<UserControl.CommandBindings>
<CommandBinding Command="DataControls:ParameterCollectionViewModel.UpdateCollection"
CanExecute="???"
Executed="???"
/>
</UserControl.CommandBindings>
DataControls:ParameterCollectionViewModel.UpdateCollection
是静态 RoatedCommand
,但数据上下文的类型为 ParameterCollectionViewModel
(并非如此)事项)。
如何使数据上下文中的事件处理程序成为命令绑定中的事件处理程序?
I could be going about this the wrong way. I am trying to put as little code behind in the view as possible, so I want the CanExecute
and Executed
events of my command binding to be handled in my viewmodel, which is my data context.
I'm sure I'm missing something really simple, but I can't think how to do it.
The XAML for the command binding:
<UserControl.CommandBindings>
<CommandBinding Command="DataControls:ParameterCollectionViewModel.UpdateCollection"
CanExecute="???"
Executed="???"
/>
</UserControl.CommandBindings>
DataControls:ParameterCollectionViewModel.UpdateCollection
is a static RoutedCommand
, but the data context is of type ParameterCollectionViewModel
(not that it matters).
How do I get the event handlers in my data context to be the event handlers in the command binding?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用
DelegateCommand
或类似的东西(Microsoft Prism 源代码中也存在实现)。
You would probably be much happier if you used a
DelegateCommand
or something similar (an implementation also exists in the Microsoft Prism sources).简而言之,您需要将命令添加到数据上下文中。如果数据上下文是 DependencyObject,则只需将新命令添加到 Commands 属性即可。
如果数据上下文不是依赖属性,我最好希望它是一个视图模型。在这种情况下,您可以使用 RelayCommand 或 DelegateCommand(实际上,它们是相同的)。
我是 Catel 的开发人员之一,Catel 是一个开源 MVVM 框架,也支持命令。您可以找到有关 Catel 命令 的详细信息。
Simply said, you need to add the command to your datacontext. If the datacontext is a DependencyObject, you can simply add a new command to the Commands property.
If the datacontext is not a dependency property, I better hope it's a view model. In that case, you can use RelayCommand or DelegateCommand (actually, they are both the same).
I am one of the developers of Catel, an open-source MVVM framework, which also supports commands. You can find more information about Catel commands.