문제

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;
   }
}
도움이 되었습니까?

해결책 2

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();
      }
   }
}

다른 팁

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>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top