WPF:当绑定命令无法执行时如何使按钮(splitbutton)启用?

发布于 2024-08-13 02:37:34 字数 820 浏览 7 评论 0原文

我正在构建一个由两个重叠按钮和弹出窗口组成的自定义拆分按钮 - 像这样(图像显示一些通用拆分按钮):

SplitButton http://windowsclient.net/wf/blogs/jfoscoding/splitbutton.PNG

我的 SplitButton 在单击带有箭头的右侧时打开弹出窗口,并在主按钮时执行 SplitButton.Command被点击。

然而,就我而言,有时默认操作无法执行,在这些情况下,当单击主按钮时,我想打开弹出窗口。

我已经完成了它,但问题是这样的:

  • SplitButton 是
  • ControlTemplate 中 ToggleButton 的子类,我有一个 ToggleButton (底部的)和 Button (较短,覆盖在顶部)
  • Button.Command="{ TemplateBinding Command}" (这样我就可以...
  • 问题:当命令无法执行时,整个SplitButton 被禁用。

我希望继续使用相同的命令,但要覆盖按钮的行为,以便在 Command.CanExecute() 返回 false 时它不会被禁用。我该怎么做?

谢谢你!

I am building a custom splitbutton that consists of two overlapping buttons, and popup - like this (image shows some generic splitbutton):

SplitButton http://windowsclient.net/wf/blogs/jfoscoding/splitbutton.PNG

My SplitButton opens popup whan a right side with arrow is clicked, and executes SplitButton.Command when a main button is clicked.

In my case however sometimes the default action cannot execute, and in those cases, when a main button is clicked, I'd like to open the popup instead.

I have accomplished it, but the problem is this:

  • the SplitButton is subclassed from ToggleButton
  • in a ControlTemplate, I have a ToggleButton (the bottom one), and Button (shorter, overlaid on the top)
  • Button.Command="{TemplateBinding Command}" (so I can do <SplitButton Command="{Binding MyDefaultAction}">...)
  • problem: when a command cannot execute, the whole SplitButton gets disabled.

I'd like to keep having the same command, but to override the button's behavior so that it doesn't become disabled when Command.CanExecute() returns false. How can I do this?

Thank you!

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

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

发布评论

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

评论(1

潦草背影 2024-08-20 02:37:35

Command 是一个依赖属性,我很确定它会被可视化树中它下面的任何控件继承。因此,对于您不想禁用的按钮,将命令属性设置为不同的命令或将其设置为 null 也可能有效。

或者,

创建您自己的依赖属性...就像这个

依赖属性

Command is a dependency property and I'm pretty sure it is inherited by any control beneath it in the visual tree. So for the button you don't want disabled, set the command property to a different command or setting it to null may also work.

Alternatively,

Create your own dependency property...like this one

Dependency Propert

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