从 WPF 中的命令内访问控件

发布于 2024-09-04 14:59:04 字数 472 浏览 3 评论 0原文

我有一个 WPF 应用程序,其菜单正在使用命令功能。一切都连接得很好,当我单击菜单中的按钮时,命令就会运行。但是,我无法获取按钮的 IsEnabled 状态以遵守命令的 CanExecute 部分。

一项挑战是命令需要查看您在 UI 中执行的操作。例如,只有在选择 ListBox 中的某些项目时才应使用一个命令,因此我需要获取对 ListBox 的引用...但由于该命令在我的视图模型(MVVM 模式)中公开,因此它没有访问 UI(顺便说一句,菜单位于一个用户控件 [parent=mainwindow] 中,而 ListBox 位于另一个用户控件 [parent=mainwindow] 中)。

此外,即使我对命令的 CanExecute 方法进行硬编码以返回 false,按钮的 Enabled 属性也不会更改...命令不会触发,但也不会更改...令人沮丧。我假设我需要修改/引发 CanExecuteChanged 事件,但我不确定何时应该这样做(找不到好的示例)。

反馈?

I've got a WPF app who's menu is using the commanding capabilities. Everything is wired up just fine and when I click the buttons in the menu, the commands run. However I'm having trouble getting the button's IsEnabled status to respect the CanExecute part of my commands.

One challenge is the commands need to see what you're doing in the UI. For instance, one command should only be available when certain items in a ListBox are selected so I need to get a reference to the ListBox... but since the command is exposed in my view model (MVVM pattern), it doesn't have access to the UI (BTW, the menu is in one user control [parent=mainwindow] while the ListBox is in another user control [parent=mainwindow]).

In addition, even when I hard code the command's CanExecute method to return false, the Enabled property of the button doesn't change... the command won't fire, but it won't change... frustrating. I assume I need to modify/raise the CanExecuteChanged event, but I'm not sure when I should be doing that (can't find a good sample).

Feedback?

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

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

发布评论

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

评论(2

杀お生予夺 2024-09-11 14:59:04

尝试 MVVMLight 中的 Messenger 类。它有助于 ViewModel 之间的通信。

并尝试一下:

在 ListBox 的 ViewModel 中有一个 SelectedItem 属性。当属性改变时广播消息。在菜单的 ViewModel 中注册此消息。将 CanExecute 方法中的 SelectedItem 属性用于您的逻辑。

Try the Messenger class from MVVMLight. It helps in communicating between ViewModels.

And give this a try:

Have a SelectedItem property in your ListBox's ViewModel. Broadcast messages while the property changes. Register for this message in the menu's ViewModel. Use the SelectedItem property in your CanExecute method for your logic.

蒲公英的约定 2024-09-11 14:59:04

通常,您将绑定 MenuItem/Button 的 Command 属性,无论什么 - 这意味着您仍然可以使用 CommandParameter - 然后可以将其绑定到其他一些控件。但是,当两个视图彼此不认识时,您需要它们之间的某种中介(即两个视图都可以访问的视图模型 - 将列表框 SelectedItem/SelectedItems 双向绑定到属性 - 并让 CommandParameter单向绑定到同一属性)。

至于何时触发 CanExecuteChanged 事件 - 只要 CanExecute 方法中包含的逻辑发生更改,您就应该触发该事件。如果它始终为 false,则永远不应该触发该事件,因为它会在设置命令参数时读取初始值。 '

如果您的按钮行为异常 - 检查 IsEnabled 属性是否受样式影响或直接设置。

希望这有帮助。

Normally, you would bind the Command Property of the MenuItem/Button whatever - that means you still have the CommandParameter to work with - this can then be bound to some other control. However, when the two views do not know each-other, you need som kind of mediator between them (ie. a viewmodel that both views can access - have the listbox SelectedItem/SelectedItems bound to a property two-ways - and let the CommandParameter bind to the same property one-way).

As for when to fire the CanExecuteChanged event - you should fire that whenever there is a change in the logic contained in the CanExecute-method. If it is always false, you should never fire the event, as it will read the initalvalue when the Command-parameter is set. '

If your button is behaving oddly - check to see if the IsEnabled property is influenced by Styles or set directly.

Hope this helps.

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