在您的导航窗口 XAML 中,您可以添加以下内容:
<NavigationWindow.InputBindings>
<KeyBinding Key="Back" Command="NotACommand" />
<KeyBinding Key="Next" Command="NotACommand" />
<KeyBinding Key="BrowserBack" Command="NotACommand" />
<KeyBinding Key="BrowserForward" Command="NotACommand" />
<KeyBinding Key="Left" Modifiers="Alt" Command="NotACommand" />
<KeyBinding Key="Right" Modifiers="Alt" Command="NotACommand" />
</NavigationWindow.InputBindings>
在代码中你可以这样做:
navigationWindow.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.Back, ModifierKeys.None));
navigationWindow.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.Next, ModifierKeys.None));
navigationWindow.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.BrowserBack, ModifierKeys.None));
navigationWindow.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.BrowserForward, ModifierKeys.None));
navigationWindow.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.Left, ModifierKeys.Alt));
navigationWindow.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.Right, ModifierKeys.Alt));
还有其他可以阻止的键,例如 BrowserHome 和 BrowserRefresh 。
如果正在显示导航 UI,这可以防止热键,而不是鼠标导航。如果您只想以编程方式控制导航,则应使用ShowsNavigationUI="False"
XAML(作为 NavigationWindow 标记的参数)或mainWindow.ShowsNavigationUI = false;
代码隐藏导航 UI。
此外,您可以像我在上面对 KeyBindings 所做的那样防止 MouseBindings,添加带有 MouseAction 属性集的新 MouseBinding 对象。