Lars Boele
Lars Boele

Reputation: 375

How disable navigation shortcuts in frame c# WPF

How can I disable the navigation shortcuts in a frame (for example the "Backspace" for navigation backward and "Alt+Right arrow" for navigation forward).

I want to use other keyboard functions, so I want to disable the navigation shortcuts of the frame.

Who can help me?

Upvotes: 14

Views: 11018

Answers (6)

samy nathan
samy nathan

Reputation: 91

Webview2 edge = new Webview2();

public Form1()
{
    InitializeComponent();
    edge.KeyDown += EdgeWebBrowser_KeyDown;
}

private void EdgeWebBrowser_KeyDown(object sender, KeyEventArgs e)
{
    if (((e.KeyCode == Keys.Left) && (GetAsyncKeyState(Keys.Menu) < 0)) || ((e.KeyCode == Keys.Right) && (GetAsyncKeyState(Keys.Menu) < 0)) || (e.KeyCode == Keys.F5) || ((e.KeyCode == Keys.R) && (GetAsyncKeyState(Keys.ControlKey) < 0)))
        {
            e.Handled = true;
        }
}

Upvotes: 0

Poppyto
Poppyto

Reputation: 614

The real answer to disable all shortcuts in WPF Frame is:

foreach (var vNavigationCommand in new RoutedUICommand[] 
                {   NavigationCommands.BrowseBack,
                    NavigationCommands.BrowseForward,
                    NavigationCommands.BrowseHome,
                    NavigationCommands.BrowseStop,
                    NavigationCommands.Refresh,
                    NavigationCommands.Favorites,
                    NavigationCommands.Search,
                    NavigationCommands.IncreaseZoom,
                    NavigationCommands.DecreaseZoom,
                    NavigationCommands.Zoom,
                    NavigationCommands.NextPage,
                    NavigationCommands.PreviousPage,
                    NavigationCommands.FirstPage,
                    NavigationCommands.LastPage,
                    NavigationCommands.GoToPage,
                    NavigationCommands.NavigateJournal })
{
    ctlFrame.CommandBindings.Add(new CommandBinding(vNavigationCommand, (sender, args) => { }));
}

Upvotes: 1

Anthony Hayward
Anthony Hayward

Reputation: 2322

See this answer for how to disable the keyboard shortcuts:

Disable backspace in wpf

That doesn't work for the back and forward navigation mouse buttons. To prevent that, it seems you need to put a handler on the Navigating event and cancel it if you don't want it.

For example, to totally disable forward navigation:

In .xaml:

<Frame Navigating="HandleNavigating" />

In code behind:

    void HandleNavigating(Object sender, NavigatingCancelEventArgs e)
    {
        if (e.NavigationMode == NavigationMode.Forward)
        {
            e.Cancel = true;
        }
    }

Upvotes: 5

Osama Javed
Osama Javed

Reputation: 1442

there is a more elegant solution where Attached behaviours can be used to disable navigation without actually extending a frame.

create an attached-behaviour :

using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace A
{
    public static class DisableNavigation
    {
        public static bool GetDisable(DependencyObject o)
        {
            return (bool)o.GetValue(DisableProperty);
        }
        public static void SetDisable(DependencyObject o, bool value)
        {
            o.SetValue(DisableProperty, value);
        }

        public static readonly DependencyProperty DisableProperty =
            DependencyProperty.RegisterAttached("Disable", typeof(bool), typeof(DisableNavigation),
                                                new PropertyMetadata(false, DisableChanged));



        public static void DisableChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var frame = (Frame)sender;
                       frame.Navigated += DontNavigate;
            frame.NavigationUIVisibility = NavigationUIVisibility.Hidden;
        }

        public static void DontNavigate(object sender, NavigationEventArgs e)
        {
            ((Frame)sender).NavigationService.RemoveBackEntry();
        }
    }
}

And in the xaml add this whenever you use a frame :

<Frame beha:DisableNavigation.Disable="True" />

and at the top of the xaml add the import :

xmlns:beha="clr-namespace:A"

Upvotes: 18

paparazzo
paparazzo

Reputation: 45096

What I do is host the content in ContentControl.

Upvotes: 1

Eddie
Eddie

Reputation: 700

The frame it's self provides no method of disabling navigation. It only provides a means to hide the navigation controls. You can however inherit from the Frame class and make some modifications to it yourself. The following example removes the last page from the BackStack every time the page navigates. Thus ensuring the frame can never navigate backwards as it does not know which page was last.

class NoNavFrame : Frame
{
    public NoNavFrame()
    {
        this.Navigated += new System.Windows.Navigation.NavigatedEventHandler(NoNavFrame_Navigated);
    }

    void NoNavFrame_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        this.NavigationService.RemoveBackEntry();
    }
}

Then you can include this in XAML as follows...

    <myControls:NoNavFrame x:Name="myFrame" NavigationUIVisibility="Hidden" />

Upvotes: 0

Related Questions