WPF How to disable NavigationWindow commands

663 views Asked by At

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?

1

There are 1 answers

0
Ceottaki On

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.