WPF:命令被禁用

发布于 2024-10-29 22:59:08 字数 1185 浏览 2 评论 0原文

我有一个 WPF UserControl,其中包含一个带有 5 个按钮的工具栏。其中 4 个按钮的命令在父窗口中处理,但第五个按钮的命令由 UC 本身处理。似乎无论我尝试什么,该按钮总是被禁用。这是我的代码:

在用户控件中...

<UserControl.CommandBindings>
<CommandBinding Command="loc:MyAppCommands.OpenUrl" Executed="OpenUrl" />
</UserControl.CommandBindings>

有问题的按钮...

<l49ui:ImageButton 
  Command="loc:MyAppCommands.OpenUrl" 
  CommandParameter="{Binding}" 
  Text="{lex:LocText MyApp.Core:Strings:OpenUrl}"  
  ShowText="False" 
  ToolTip="{lex:LocText MyApp.Core:Strings:OpenUrl}" 
  Style="{StaticResource ToolBarButton}" 
  Image="{Binding Url,Converter={StaticResource UrlNotNullConverter},ConverterParameter=image}" 
  IsEnabled="true" /> 

用户控件的 datacoontext 是基于列表选择设置的;所以我将 WIndow->UserControl 绑定到 ListBox->TabControl->Toolbar 按钮

图像根据命令参数(在本例中为 Url)是否为空而变化,并且这有效。但是,该按钮始终处于禁用状态。导致它启用的唯一原因是切换到父选项卡控件中的另一个选项卡,然后使用按钮返回到选项卡。

我尝试过调用 CommandManager.InvalidateRequerySuggested();当数据上下文更改并为命令添加“CanAlwaysExecute”处理程序时,但当我调用 CommandManager.InvalidateRequerySuggested(); 时,不会执行 CanExecute 处理程序。

I have a WPF UserControl, which contains a toolbar with 5 buttons. The commands for 4 of the buttons is handled in the parent window, but the fifth button's command is handled by the UC itself. It seems no matter what I try, the button is always disabled. This is my code:

In the user control...

<UserControl.CommandBindings>
<CommandBinding Command="loc:MyAppCommands.OpenUrl" Executed="OpenUrl" />
</UserControl.CommandBindings>

The button in question...

<l49ui:ImageButton 
  Command="loc:MyAppCommands.OpenUrl" 
  CommandParameter="{Binding}" 
  Text="{lex:LocText MyApp.Core:Strings:OpenUrl}"  
  ShowText="False" 
  ToolTip="{lex:LocText MyApp.Core:Strings:OpenUrl}" 
  Style="{StaticResource ToolBarButton}" 
  Image="{Binding Url,Converter={StaticResource UrlNotNullConverter},ConverterParameter=image}" 
  IsEnabled="true" /> 

The datacoontext for the user control is set based on a list selection; so I have WIndow->UserControl tied to ListBox->TabControl->Toolbar buttons

The image changes based on whether the commandparameter (Url in this case) is empty, and this works. However, the button is always disabled. The only thing which causes it to enable is switching to another tab in a pareent tab control, then back to to tab with the button.

I've tried calling CommandManager.InvalidateRequerySuggested(); when the datacontext changes and adding a "CanAlwaysExecute" handler for the command, but the CanExecute handler isn't executed when I call CommandManager.InvalidateRequerySuggested();.

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

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

发布评论

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

评论(2

那些过往 2024-11-05 22:59:08

这可能是焦点范围的问题。尝试显式指定 CommandTarget 属性你的按钮。将其设置为用户控件内应引发 Execute 和 CanExecute 事件的元素(可能是用户控件内的根元素)。

It might be an issue with focus scopes. Try specifying the CommandTarget property explicitly on your button. Set it to an element inside your user control on which the Execute and CanExecute events should be raised (probably root element inside the user control).

丶情人眼里出诗心の 2024-11-05 22:59:08

您是否可以在父控件中处理此命令的 CanExecute 事件?如果事件在树中向上冒泡,则可能在另一个地方用“false”返回值处理它,因此该按钮被禁用。我尝试处理 UserControl 中的 CanExecute 事件,并在那里设置断点以进一步调查。

Are you maybe handling the CanExecute event for this command in a parent control? If the event bubbles up the tree, maybe in another place it is handled with a 'false' return value, so the button is disabled. I'd try to handle the CanExecute event within the UserControl, and set a breakpoint there to investigate further.

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