首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >KeyBindings要控制的CanExecute

KeyBindings要控制的CanExecute
EN

Stack Overflow用户
提问于 2011-11-29 03:24:35
回答 2查看 1K关注 0票数 2

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

相关代码-

代码语言:javascript
复制
<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>

代码隐藏-

代码语言:javascript
复制
private void DecreaseZoom_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
   if (((DocumentViewer)e.Source).PageViews.Count >= 3)
   {
       e.CanExecute = false;
       e.ContinueRouting = false;
       e.Handled = true;
   }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-29 21:02:25

我解决了扩展DocumentViewer和覆盖OnDecreaseZoomCommand方法的问题。我尝试使用自定义命令,但如果我使用快捷键"Ctrl -“,它的事件处理程序不会被击中。但这对我很有效-

代码语言:javascript
复制
public class ExtendedDocumentViewer : DocumentViewer
{
   protected override void OnDecreaseZoomCommand()
   {
      if (PageViews.Count < 3)
      {
         base.OnDecreaseZoomCommand();
      }
   }
}
票数 1
EN

Stack Overflow用户

发布于 2011-11-29 15:16:32

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

代码语言:javascript
复制
 <KeyBinding.Gesture>
   <CustomInputGesture/>
 </KeyBinding.Gesture>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8301139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档