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) => { }));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With