CanExecute 用于 KeyBindings 来控制

发布于 2024-12-18 08:24:28 字数 1329 浏览 3 评论 0原文

我有一个带有默认工具栏按钮的reportViewer,用于减少绑定到命令NavigationCommands.DecreaseZoom的缩放。我想在某些情况下禁用它,所以我绑定 CanExecute 方法为该命令返回 false,该命令工作得很好并按预期禁用按钮。但是,如果我使用快捷键“Ctrl + Subtract key”,缩小仍然有效。我尝试将 KeyBinding 设置为同一命令,假设 CanExecute 可以工作,但事实并非如此。由于 KeyBinding 中未提供 CanExecute。有人可以建议我如何在某些情况下禁用 KeyGesture“Ctrl -”(CanExecute 中的逻辑)而不是永久禁用。

相关代码 -

<DocumentViewer Name="documentViewer1"
                        Margin="0,0,0,30"
                        Style="{DynamicResource DocumentViewerStyle1}">
   <DocumentViewer.CommandBindings>
        <CommandBinding Command="NavigationCommands.DecreaseZoom"
                        CanExecute="DecreaseZoom_CanExecute" />
   </DocumentViewer.CommandBindings>
   <DocumentViewer.InputBindings>
        <KeyBinding Command="NavigationCommands.DecreaseZoom"
                    Key="OemMinus"
                    Modifiers="Control" />
    </DocumentViewer.InputBindings>
</DocumentViewer>

代码隐藏 -

private void DecreaseZoom_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
   if (((DocumentViewer)e.Source).PageViews.Count >= 3)
   {
       e.CanExecute = false;
       e.ContinueRouting = false;
       e.Handled = true;
   }
}

I have a reportViewer with default toolbar button for decrease zoom binded to command NavigationCommands.DecreaseZoom. I want to disable it in some situation so i bind CanExecute method to return false for that command which works perfectly fine and disable the button as expected. But, still zoom out works if i use shortcut key "Ctrl + Subtract key". I tried to set KeyBinding to the same command assuming CanExecute will work but it doesn't. Since, CanExecute is not provided in KeyBinding. Can someone suggest how can i disable KeyGesture "Ctrl -" for some situation(logic in CanExecute) and not permanently.

Relevant code -

<DocumentViewer Name="documentViewer1"
                        Margin="0,0,0,30"
                        Style="{DynamicResource DocumentViewerStyle1}">
   <DocumentViewer.CommandBindings>
        <CommandBinding Command="NavigationCommands.DecreaseZoom"
                        CanExecute="DecreaseZoom_CanExecute" />
   </DocumentViewer.CommandBindings>
   <DocumentViewer.InputBindings>
        <KeyBinding Command="NavigationCommands.DecreaseZoom"
                    Key="OemMinus"
                    Modifiers="Control" />
    </DocumentViewer.InputBindings>
</DocumentViewer>

Code behind -

private void DecreaseZoom_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
   if (((DocumentViewer)e.Source).PageViews.Count >= 3)
   {
       e.CanExecute = false;
       e.ContinueRouting = false;
       e.Handled = true;
   }
}

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

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

发布评论

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

评论(2

夜未央樱花落 2024-12-25 08:24:28

您可以为此创建自定义命令,也可以创建自己的 InputGesture,并覆盖其行为,

 <KeyBinding.Gesture>
   <CustomInputGesture/>
 </KeyBinding.Gesture>

You can either create your custom command for this or you can create your own InputGesture, and override its behavior,

 <KeyBinding.Gesture>
   <CustomInputGesture/>
 </KeyBinding.Gesture>
用心笑 2024-12-25 08:24:28

我解决了扩展 DocumentViewer 和重写方法 OnDecreaseZoomCommand 的问题。我尝试使用自定义命令,但如果我使用快捷键“Ctrl -”,则其事件处理程序不会被命中。但这对我有用 -

public class ExtendedDocumentViewer : DocumentViewer
{
   protected override void OnDecreaseZoomCommand()
   {
      if (PageViews.Count < 3)
      {
         base.OnDecreaseZoomCommand();
      }
   }
}

I solved my problem extending DocumentViewer and overriding method OnDecreaseZoomCommand. I tried using Custom Command but its event handler is not getting hit in case i use shortcut key "Ctrl -". But this works for me -

public class ExtendedDocumentViewer : DocumentViewer
{
   protected override void OnDecreaseZoomCommand()
   {
      if (PageViews.Count < 3)
      {
         base.OnDecreaseZoomCommand();
      }
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文