简体   繁体   中英

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?

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"

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;
        }
    }

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) => { }));
}

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" />

我所做的是在ContentControl中托管内容。

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