CanExecute 用于 KeyBindings 来控制
我有一个带有默认工具栏按钮的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;
}
}
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以为此创建自定义命令,也可以创建自己的 InputGesture,并覆盖其行为,
You can either create your custom command for this or you can create your own InputGesture, and override its behavior,
我解决了扩展 DocumentViewer 和重写方法 OnDecreaseZoomCommand 的问题。我尝试使用自定义命令,但如果我使用快捷键“Ctrl -”,则其事件处理程序不会被命中。但这对我有用 -
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 -