WPF:如何在不禁用箭头键导航的情况下禁用选项卡导航?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4210659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:20:15  来源:igfitidea点击:

WPF: How to disable tab navigation without also disabling arrow key navigation?

wpfnavigationtabstop

提问by Meh

I have set IsTabStopto false on all controls in my window, so that when I press the Tab key, the focus doesn't move (I need the Tab key for something else). But doing this breaks arrow key navigation - I click on an item in a ListViewand then pressing up/down doesn't change the selected item anymore.

IsTabStop在窗口中的所有控件上都设置为 false,因此当我按下 Tab 键时,焦点不会移动(我需要 Tab 键来做其他事情)。但是这样做会破坏箭头键导航 - 我单击 a 中的一个项目ListView,然后按上/下不再更改所选项目。

Is there a way to disable tab navigation, but without touching arrow key navigation? They seem to be related.

有没有办法禁用选项卡导航,但不触摸箭头键导航?他们似乎是有关系的。

I tried setting IsTabStopto true and TabNavigationto false, but it doesn't work either.

我尝试设置IsTabStop为 true 和TabNavigationfalse,但它也不起作用。

<ListView ItemContainerStyle="{StaticResource ItemCommon}" IsTabStop="False">
    <ListView.Resources>
        <Style x:Key="ItemCommon">
            <Setter Property="IsTabStop" Value="False"/>
            <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
            <Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Cycle"/>
        </Style>
    </ListView.Resources>
</ListView>

回答by Ed Gonzalez

On your window (or some ancestor of the controls you don't want tab to work on) swallow the tab key.

在您的窗口(或您不想使用 Tab 的控件的某些祖先)上吞下 Tab 键。

You can swallow it by attaching to the PreviewKeyDown event and set e.Handled = true when the key is a tab.

您可以通过附加到 PreviewKeyDown 事件并在键是选项卡时设置 e.Handled = true 来吞下它。

Pure Code Behind:

纯代码背后:

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.PreviewKeyDown += MainWindowPreviewKeyDown;
        }

        static void MainWindowPreviewKeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Tab)
            {
                e.Handled = true;
            }
        }
    }

You can also set a Keyboard handler as such:

您还可以这样设置键盘处理程序:

<Window x:Class="TabSwallowTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        Keyboard.PreviewKeyDown="Window_PreviewKeyDown" >

    <StackPanel>
        <TextBox Width="200" Margin="10"></TextBox>
        <TextBox Width="200" Margin="10"></TextBox>
    </StackPanel>
</Window>

but you'll need a corresponding event handler:

但你需要一个相应的事件处理程序:

   private void Window_PreviewKeyDown(object sender, KeyEventArgs e)

    {
        if (e.Key == Key.Tab)
        {
            e.Handled = true;
        }
    }

回答by jpierson

I believe what you want is to set the KeyboardNavigation.TabNavigationattached property to Onceon your ListView. I've done this with a templated ItemsControl and it seems to give me the behavior that I would expect from like a ListBox where a tab into the control will select the first item but an additional tab will tab right out of the listbox and onto the next control.

我相信您想要的是在 ListView 上将KeyboardNavigation.TabNavigation附加属性设置为一次。我已经使用模板化的 ItemsControl 完成了此操作,它似乎给了我我期望的行为,就像 ListBox 一样,其中一个选项卡进入控件将选择第一个项目,但另一个选项卡将直接从列表框跳出并进入下一个控制。

So following this method your example may be able to be shortend down to just this.

因此,按照此方法,您的示例可能可以缩短为这样。

<ListView ItemContainerStyle="{StaticResource ItemCommon}"
          KeyboardNavigation.TabNavigation="Once" />

I haven't tested this with the ListView control however but I wouldn't be surprised if it works for you.

我还没有用 ListView 控件对此进行过测试,但是如果它对您有用,我不会感到惊讶。