2

コマンドにバインドされたズームを減らすためのデフォルトのツールバーボタンを備えたreportViewerがありますNavigationCommands.DecreaseZoom。ある状況でそれを無効にしたいので、メソッドをバインドCanExecuteして、そのコマンドに対してfalseを返します。これは完全に正常に機能し、期待どおりにボタンを無効にします。ただし、ショートカットキーを使用すると、ズームアウトは機能します"Ctrl + Subtract key"。CanExecuteが機能すると仮定して同じコマンドを設定しようとしましKeyBindingたが、機能しません。以来、CanExecuteはKeyBindingで提供されていません。誰かが、永続的ではなく、特定の状況(CanExecuteのロジック)でKeyGesture「Ctrl-」を無効にする方法を提案できますか?

関連コード-

<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;
   }
}
4

2 に答える 2

1

このためのカスタムコマンドを作成するか、独自のInputGestureを作成して、その動作をオーバーライドすることができます。

 <KeyBinding.Gesture>
   <CustomInputGesture/>
 </KeyBinding.Gesture>
于 2011-11-29T07:16:32.000 に答える
1

DocumentViewerを拡張し、メソッドOnDecreaseZoomCommandをオーバーライドする問題を解決しました。カスタムコマンドを使用してみましたが、ショートカットキー「Ctrl-」を使用してもイベントハンドラーがヒットしません。しかし、これは私にとってはうまくいきます-

public class ExtendedDocumentViewer : DocumentViewer
{
   protected override void OnDecreaseZoomCommand()
   {
      if (PageViews.Count < 3)
      {
         base.OnDecreaseZoomCommand();
      }
   }
}
于 2011-11-29T13:02:24.850 に答える