简体   繁体   中英

WPF How to disable NavigationWindow commands

I use pages and 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?

In your navigation window XAML you can add this:

<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.

That prevents hotkeys, not mouse navigation if the navigation UI is being displayed. 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; 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM