Wpf 如何防止选项卡导航到控件,同时允许定向导航到该控件

Wpf 如何防止选项卡导航到控件,同时允许定向导航到该控件,wpf,focus,tabbing,keyboard-navigation,Wpf,Focus,Tabbing,Keyboard Navigation,在WPF(MVVM,无代码隐藏)中,假设我有以下xaml: <StackPanel> <Button>Normal 1</Button> <Button>Special</Button> <!--This button should not tab focus, but still focus via arrow keys--> <Button>Normal 2</Button>

在WPF(MVVM,无代码隐藏)中,假设我有以下xaml:

<StackPanel>
    <Button>Normal 1</Button>
    <Button>Special</Button> <!--This button should not tab focus, but still focus via arrow keys-->
    <Button>Normal 2</Button>
</StackPanel>

正常1
特别的
正常2
默认情况下,您可以(1)在按钮之间使用tab键,或(2)在按钮之间使用上/下箭头。我想让其中一个按钮--特殊按钮--通过tab键不可聚焦,但通过up/down键仍可聚焦。我尝试了
IsTabStop=“False”
,但这也阻止了通过定向导航(向上/向下箭头)聚焦该按钮

如何从选项卡导航中删除单个按钮,同时继续允许定向导航?所有其他控件的选项卡和箭头应不受影响

我尝试了
IsTabStop
KeyboardNavigation.TabNavigation
KeyboardNavigation.directionaldnavigation
的组合,但没有成功。也许我没有找到合适的组合。或者可能还有另一种方法。想法


编辑:好的,我要增加一笔赏金。要明确的是,我正在寻找一个MVVM,没有代码隐藏的方式来装饰控件(例如,通过样式、附加属性、附加行为等),以便将其从选项卡顺序中删除,同时保持有效的定向导航目标。不接受在窗口(或类似窗口)中硬编码控件知识。这需要是一个通用的、可重用的解决方案。类似于:

我是如何做到的:

<Window x:Class="MvvmLight1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:ignore="http://www.ignore.com"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:command="http://www.galasoft.ch/mvvmlight"
    mc:Ignorable="d ignore"
    Height="300"
    Width="300"
    DataContext="{Binding Main, Source={StaticResource Locator}}">
<i:Interaction.Triggers>
    <i:EventTrigger EventName="KeyDown">
        <command:EventToCommand Command="{Binding Mode=OneWay,Path=KeyDownCommand}" PassEventArgsToCommand="True"/>
    </i:EventTrigger>
</i:Interaction.Triggers>
<Grid x:Name="LayoutRoot">
    <StackPanel>
        <Button>Normal 1</Button>
        <Button>Special</Button>
        <!--Should not tab focus, yet still focus via arrow keys-->
        <Button>Normal 2</Button>
    </StackPanel>
</Grid>

正常1
特别的
正常2

我使用的是mvvmlight,EventToCommand用于截获KeyDown事件并将其定向到ViewModel中的以下命令:

 private RelayCommand<KeyEventArgs> _keyDownCommand;
    public RelayCommand<KeyEventArgs> KeyDownCommand
    {
        get
        {
            return _keyDownCommand
                ?? (_keyDownCommand = new RelayCommand<KeyEventArgs>(
                (s) =>
                {
                    if (s.Key == Key.Tab)
                    {
                        s.Handled = true;
                    }
                }));
        }
    }
private RelayCommand\u keydown命令;
公共中继命令keydown命令
{
得到
{
return\u keydown命令
?(_keydown命令=新继电器命令(
(s) =>
{
如果(s.Key==Key.Tab)
{
s、 已处理=正确;
}
}));
}
}

那个选项怎么样。将特殊按钮插入另一个容器中,并将KeyboardNavigation.TabNavigation=“None”放在该容器上。我刚刚试过,看起来我能达到你的要求

<StackPanel >
  <Button>Normal 1</Button>
  <StackPanel KeyboardNavigation.TabNavigation="None">
    <Button>Special</Button><!--This button should not tab focus, but still focus via arrow keys-->
  </StackPanel>
  <Button>Normal 2</Button>
</StackPanel>

正常1
特别的
正常2

这应该会有帮助:[WPF:如何在不禁用箭头键导航的情况下禁用选项卡导航?][1][1]:@Joseph我发现了这个问题,但那个人的情况不同(他们使用的是listview),建议的解决方案都不能解决我的问题。另外,公认的解决方案使用代码隐藏,并要求窗口了解其中所有控件的制表符行为——我希望控件指示自己的行为。谢谢,但这会禁用整个窗口的制表符。我正在寻找一种方法来禁用一个控件的选项卡。什么控件!你是说StackPanel?就一个按钮。特殊按钮。所有其他按钮仍应为选项卡式。问题是:如何从选项卡顺序中删除控件(例如按钮),同时将其保留为有效的定向导航目标。这是正确的解决方案;
TabNavigation
属性控制元素子元素的逻辑导航行为,因此在OP中,“特殊”按钮应该包装在另一个容器中。太棒了!我的真实场景更复杂,但这是我需要的想法/信息。在自定义控件的样式中,我设置了
Focusable=False
KeyboardNavigation.TabNavigation=None
。然后,在自定义控件的控件模板中,我设置了
Focusable=True
,它完全达到了我想要的效果。但是如果你点击按钮,焦点仍然会回到按钮上(我使用的是MaterialDesignToolkit)。对这个问题有什么看法?