我有一个带有默认工具栏按钮的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;
}
}
答案 0 :(得分:1)
您可以为此创建自定义命令,也可以创建自己的InputGesture,并覆盖其行为,
<KeyBinding.Gesture>
<CustomInputGesture/>
</KeyBinding.Gesture>
答案 1 :(得分:1)
我解决了扩展DocumentViewer和重写方法OnDecreaseZoomCommand的问题。我尝试使用自定义命令,但如果我使用快捷键“Ctrl - ”,它的事件处理程序不会被命中。但这对我有用 -
public class ExtendedDocumentViewer : DocumentViewer
{
protected override void OnDecreaseZoomCommand()
{
if (PageViews.Count < 3)
{
base.OnDecreaseZoomCommand();
}
}
}