Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 205
Lars Boele Avatar asked Jun 16 '11 06:06

Lars Boele


3 Answers

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"
like image 56
Osama Javed Avatar answered Oct 21 '22 00:10

Osama Javed


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;
        }
    }
like image 4
Anthony Hayward Avatar answered Oct 21 '22 01:10

Anthony Hayward


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) => { }));
}
like image 1
Poppyto Avatar answered Oct 21 '22 00:10

Poppyto