WPF如何禁用NavigationWindow命令

时间:2013-08-10 22:30:18

标签: wpf navigationwindow

我使用页面和NavigationWindow:

navigationWindow = new NavigationWindow();
            navigationWindow.Height = 200;
            navigationWindow.Width = 100;
            navigationWindow.WindowState = WindowState.Maximized;
            page = new IntroPage();
            navigationWindow.Navigate(page);
            navigationWindow.Show();

我使用GoBack和GoForward方法进行导航,但我不想通过快捷方式(鼠标中的功能按钮等)使用它们。如何禁用这些快捷方式?

1 个答案:

答案 0 :(得分:0)

在导航窗口XAML中,您可以添加:

<NavigationWindow.InputBindings>
    <KeyBinding Key="Back" Command="NotACommand" />
    <KeyBinding Key="Next" Command="NotACommand" />
    <KeyBinding Key="BrowserBack" Command="NotACommand" />
    <KeyBinding Key="BrowserForward" Command="NotACommand" />
    <KeyBinding Key="Left" Modifiers="Alt" Command="NotACommand" />
    <KeyBinding Key="Right" Modifiers="Alt" Command="NotACommand" />
</NavigationWindow.InputBindings>

在代码中你可以这样做:

navigationWindow.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.Back, ModifierKeys.None));
navigationWindow.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.Next, ModifierKeys.None));
navigationWindow.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.BrowserBack, ModifierKeys.None));
navigationWindow.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.BrowserForward, ModifierKeys.None));
navigationWindow.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.Left, ModifierKeys.Alt));
navigationWindow.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.Right, ModifierKeys.Alt));

还可以阻止其他键,例如Br​​owserHome和BrowserRefresh。

如果正在显示导航UI,则会阻止热键,而不是鼠标导航。如果您只想以编程方式控制导航,则应在XAML中隐藏导航用户界面ShowsNavigationUI="False"(作为NavigationWindow标记的参数)或在代码中隐藏mainWindow.ShowsNavigationUI = false;

此外,您可以像上面对KeyBindings一样阻止MouseBindings,添加具有MouseAction属性集的新MouseBinding对象。