MVVM Light - RelayCommand 实例上的 CanExecute 方法始终执行

发布于 2024-09-13 18:32:01 字数 317 浏览 1 评论 0原文

我尝试使用一个最小的应用程序,只有一个文本框和一个按钮。该按钮绑定到 RelayCommand 实例,并且 CanExecute 方法只是随机返回 true 或 false。文本框文本与字符串属性绑定。

让我生气的是 CanExecute 方法“总是”被调用:焦点元素的更改、在文本框中按下的键,似乎一切都会触发对我的 CanExecute 方法的调用。 这是 mvvm light 工具包的“功能”吗?这会发生在“正常”wpf 应用程序中吗?

是的,我知道,我想我应该更多地了解 wpf 中的 commandind 系统...;-)

感谢您的回答!

大卫

I tried with a minimal application, just a textbox and a button. The button is binded to a RelayCommand instance, and the CanExecute method just return true or false with a random. The textbox text is binded with a string property.

What's making me mad is that the CanExecute method is "always" called: a change of the element focused, a key pressed in the textbox, it seems that everything fires a call to my CanExecute method.
Is this a "feature" of the mvvm light toolkit? Does this happen in a "normal" wpf application?

Yes, I know, I think I should know more about the commandind system in wpf... ;-)

Thanks for answers!

David

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

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

发布评论

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

评论(1

灼痛 2024-09-20 18:32:01

CanExecute 用于确定当前状态是否允许命令执行。这通常与 IsEnabled 属性相关联以禁用该命令。

这也应该与 ViewModel 上的一个属性相关联,该属性指示当前视图状态是否允许执行。

它与托管窗口上的任何事件触发器相关联,因为任何事件都可能导致 CanExecute 状态发生更改。

此链接确认它会在托管窗口中的任何事件上触发。

http://robburke.net /2008/04/23/wpf-command-pattern-when-does-it-query-canexecute/

为什么你认为它一直被调用是一个问题?只要绑定属性中没有任何强烈的逻辑就应该没问题。

CanExecute is used to determine if the current state allows the command to execute. This is usually tied to the IsEnabled property to disable the command.

This should also be tied to a property on your ViewModel that indicates if the current view state allows executing.

It is tied to any event trigger on the hosting window, since any event could cause a change to the CanExecute state.

This link confirms it is fired on any event in the hosting window.

http://robburke.net/2008/04/23/wpf-command-pattern-when-does-it-query-canexecute/

Why do you think it being called all the time is an issue? As long as you don't have any intense logic in the bound property it should be fine.

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