How disable navigation shortcuts in frame c# WPF
Asked Answered
B

6

14

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?

Buckwheat answered 16/6, 2011 at 6:21 Comment(0)
K
18

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"
Koppel answered 14/6, 2012 at 16:9 Comment(0)
A
5

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;
        }
    }
Apparition answered 21/8, 2014 at 7:32 Comment(1)
This simple soluting works for me. But also add code to disable back navigation: if(e.NavigationMode == NavigationMode.Back) { e.Cancel = true; }Calbert
H
1

What I do is host the content in ContentControl.

Hibben answered 9/3, 2012 at 15:58 Comment(1)
This solution won't work if the content of the Frame is a Page.Amari
H
1

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) => { }));
}
Hickson answered 3/4, 2019 at 9:20 Comment(0)
T
0

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" />
Trophy answered 23/11, 2011 at 9:40 Comment(0)
I
0
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;
        }
}
Ingle answered 21/4, 2023 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.