如何禁用框架中的导航快捷方式(例如,“Backspace”用于向后导航,“Alt +向右箭头”用于向前导航)。
我想使用其他键盘功能,所以我想禁用框架的导航快捷方式。
谁能帮帮我?
答案 0 :(得分:14)
有一个更优雅的解决方案,其中附加行为可用于禁用导航而不实际扩展框架。
创建附加行为:
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();
}
}
}
在xaml中,只要你使用框架就添加它:
<Frame beha:DisableNavigation.Disable="True" />
并在xaml的顶部添加导入:
xmlns:beha="clr-namespace:A"
答案 1 :(得分:1)
有关如何禁用键盘快捷键,请参阅此答案:
这对后退和前进导航鼠标按钮不起作用。为了防止这种情况,你似乎需要在Navigating事件上放置一个处理程序,如果你不想要它就取消它。
例如,要完全禁用前向导航:
在.xaml:
<Frame Navigating="HandleNavigating" />
在代码背后:
void HandleNavigating(Object sender, NavigatingCancelEventArgs e)
{
if (e.NavigationMode == NavigationMode.Forward)
{
e.Cancel = true;
}
}
答案 2 :(得分:0)
它自己的框架没有提供禁用导航的方法。它只提供隐藏导航控件的方法。但是,您可以继承Frame类并自行对其进行一些修改。以下示例在每次页面导航时从BackStack中删除最后一页。因此,确保框架永远不会向后导航,因为它不知道哪个页面是最后一页。
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();
}
}
然后你可以把它包含在XAML中,如下所示......
<myControls:NoNavFrame x:Name="myFrame" NavigationUIVisibility="Hidden" />
答案 3 :(得分:0)
我所做的是在ContentControl中托管内容。
答案 4 :(得分:0)
禁用WPF框架中所有快捷方式的真正答案是:
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) => { }));
}