[英]WPF How to disable NavigationWindow commands
I use pages and NavigationWindow: 我使用页面和NavigationWindow:
navigationWindow = new NavigationWindow();
navigationWindow.Height = 200;
navigationWindow.Width = 100;
navigationWindow.WindowState = WindowState.Maximized;
page = new IntroPage();
navigationWindow.Navigate(page);
navigationWindow.Show();
I navigate by using GoBack and GoForward methods, but I don't want to use them by shortcuts (function buttons in mouse etc.) How can I disable these shortcuts? 我使用GoBack和GoForward方法进行导航,但是我不想通过快捷方式(鼠标等中的功能按钮)使用它们。如何禁用这些快捷方式?
In your navigation window XAML you can add this: 在导航窗口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>
In code you could do the same like this: 在代码中,您可以这样做:
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));
There are other keys that can be prevented, such as BrowserHome and BrowserRefresh as well. 还有其他可以防止的键,例如BrowserHome和BrowserRefresh。
That prevents hotkeys, not mouse navigation if the navigation UI is being displayed. 如果正在显示导航UI,则可以防止热键,而不是鼠标导航。 If you want to control navigation only programmatically you should hide the navigation UI with
ShowsNavigationUI="False"
in XAML (as a parameter of the NavigationWindow tag) or with mainWindow.ShowsNavigationUI = false;
如果只想以编程方式控制导航,
ShowsNavigationUI="False"
在XAML中使用ShowsNavigationUI="False"
(作为NavigationWindow标签的参数)或mainWindow.ShowsNavigationUI = false;
来隐藏导航UI mainWindow.ShowsNavigationUI = false;
in code. 在代码中。
Also, you can prevent MouseBindings the same way I have done above for KeyBindings, adding new MouseBinding objects with the MouseAction property set. 另外,您可以像上面对KeyBindings所做的一样,防止MouseBindings的出现,添加具有MouseAction属性集的新MouseBinding对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.