Keybindingsが制御するためのCanexecute
-
22-10-2019 - |
質問
ズームバインドをコマンドに減らすためのデフォルトのツールバーボタンを備えたReportViewerがあります NavigationCommands.DecreaseZoom
. 。何らかの状況で無効にしたいのでバインドします CanExecute
そのコマンドのfalseを返す方法は、完全に正常に機能し、予想どおりボタンを無効にします。しかし、ショートカットキーを使用すると、まだズームアウトが機能します "Ctrl + Subtract key"
. 。設定しようとしました KeyBinding
Canexecuteが機能すると仮定して同じコマンドには機能しません。以来、Canexecuteはキービンディングでは提供されていません。誰かが、恒久的ではなく、ある程度の状況(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;
}
}
解決 2
documentViewerを拡張し、Ondecreasezoomcommandをオーバーライドするメソッドを拡張する問題を解決しました。カスタムコマンドを使用しようとしましたが、ショートカットキー「Ctrl-」を使用した場合に備えて、そのイベントハンドラーがヒットしません。しかし、これは私のために働きます -
public class ExtendedDocumentViewer : DocumentViewer
{
protected override void OnDecreaseZoomCommand()
{
if (PageViews.Count < 3)
{
base.OnDecreaseZoomCommand();
}
}
}
他のヒント
これのためにカスタムコマンドを作成するか、独自の入力を作成し、その動作をオーバーライドすることができます。
<KeyBinding.Gesture>
<CustomInputGesture/>
</KeyBinding.Gesture>
所属していません StackOverflow