簡體   English   中英

如何禁用框架c#WPF中的導航快捷方式

[英]How disable navigation shortcuts in frame c# WPF

如何禁用框架中的導航快捷方式(例如,“Backspace”用於向后導航,“Alt +向右箭頭”用於向前導航)。

我想使用其他鍵盤功能,所以我想禁用框架的導航快捷方式。

誰能幫我?

有一個更優雅的解決方案,其中附加行為可用於禁用導航而不實際擴展框架。

創建附加行為:

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"

有關如何禁用鍵盤快捷鍵,請參閱此答案:

禁用wpf中的退格鍵

這對后退和前進導航鼠標按鈕不起作用。 為了防止這種情況,你似乎需要在Navigating事件上放置一個處理程序,如果你不想要它就取消它。

例如,要完全禁用前向導航:

在.xaml中:

<Frame Navigating="HandleNavigating" />

代碼背后:

    void HandleNavigating(Object sender, NavigatingCancelEventArgs e)
    {
        if (e.NavigationMode == NavigationMode.Forward)
        {
            e.Cancel = true;
        }
    }

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

它自己的框架沒有提供禁用導航的方法。 它只提供隱藏導航控件的方法。 但是,您可以繼承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" />

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM