WPF:如何禁用选项卡导航而不同时禁用箭头键导航?

发布于 2024-10-03 09:46:10 字数 774 浏览 8 评论 0原文

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

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

我尝试将 IsTabStop 设置为 true,将 TabNavigation 设置为 false,但它也不起作用。

<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>

I have set IsTabStop to 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 ListView and then pressing up/down doesn't change the selected item anymore.

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

I tried setting IsTabStop to true and TabNavigation to false, but it doesn't work either.

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝海 2024-10-10 09:46:10

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

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

纯代码隐藏:

 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;
            }
        }
    }

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

<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>

但您需要相应的事件处理程序:

   private void Window_PreviewKeyDown(object sender, KeyEventArgs e)

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

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

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

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;
        }
    }
你げ笑在眉眼 2024-10-10 09:46:10

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

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

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

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

I believe what you want is to set the KeyboardNavigation.TabNavigation attached property to Once on 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文