我有一个带有默认工具栏按钮的reportViewer,用于绑定到命令NavigationCommands.DecreaseZoom的default。我想在某些情况下禁用它,所以我绑定了CanExecute方法,以便为该命令返回false,它可以很好地工作,并按预期禁用按钮。但是,如果我使用快捷键"Ctrl + Subtract key",仍然可以缩小。我尝试将KeyBinding设置为相同的命令,假设CanExecute可以工作,但它不能工作。因为,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;
}
}发布于 2011-11-29 21:02:25
我解决了扩展DocumentViewer和覆盖OnDecreaseZoomCommand方法的问题。我尝试使用自定义命令,但如果我使用快捷键"Ctrl -“,它的事件处理程序不会被击中。但这对我很有效-
public class ExtendedDocumentViewer : DocumentViewer
{
protected override void OnDecreaseZoomCommand()
{
if (PageViews.Count < 3)
{
base.OnDecreaseZoomCommand();
}
}
}发布于 2011-11-29 15:16:32
您可以为此创建自定义命令,也可以创建自己的InputGesture并覆盖其行为,
<KeyBinding.Gesture>
<CustomInputGesture/>
</KeyBinding.Gesture>https://stackoverflow.com/questions/8301139
复制相似问题