确保箭头键跳过 WPF 菜单中的分隔符项目?
在我的 WPF 菜单中,我有一个分隔符 (System.Windows.Controls.Separator
) 项,当通过箭头键导航菜单时,它似乎无形地接受焦点。我希望箭头导航路径能够跳过这些分隔符。我尝试将 IsEnabled
和 IsTabStop
设置为 false
,但这些似乎都没有任何效果。
在 XAML 中,如何指定应跳过分隔符项?
更新:这是我的分隔符当前样式的 XAML:
<Style TargetType="{x:Type Separator}">
<Setter Property="Focusable" Value="false" />
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="IsTabStop" Value="false" />
<Setter Property="IsHitTestVisible" Value="false" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Separator}">
<Border BorderThickness="1"
Margin="10,5,10,5">
<Border.BorderBrush>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1" >
<GradientStop Color="{DynamicResource Nui10Color}" Offset="0"/>
<GradientStop Color="{DynamicResource Nui50Color}" Offset="1"/>
</LinearGradientBrush>
</Border.BorderBrush>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
In my WPF menu I have a separator (System.Windows.Controls.Separator
) item that seems to invisibly accept focus when navigating the menu via the arrow keys. I'd like the arrow navigation path to skip these separators. I've tried setting IsEnabled
and IsTabStop
are set to false
, but neither of these seem to have any effect.
In XAML, how can I specify that separator items should be skipped?
UPDATE: Here's the currently style XAML for my separator:
<Style TargetType="{x:Type Separator}">
<Setter Property="Focusable" Value="false" />
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="IsTabStop" Value="false" />
<Setter Property="IsHitTestVisible" Value="false" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Separator}">
<Border BorderThickness="1"
Margin="10,5,10,5">
<Border.BorderBrush>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1" >
<GradientStop Color="{DynamicResource Nui10Color}" Offset="0"/>
<GradientStop Color="{DynamicResource Nui50Color}" Offset="1"/>
</LinearGradientBrush>
</Border.BorderBrush>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想通了。我需要在 MenuItem 本身而不是模板上设置 Focusable:
I figured it out. I needed to set Focusable on the MenuItem itself, not the template:
尝试对其设置
IsHitTestVisible="False"
。它应该禁止它们进行所有命中测试Try setting
IsHitTestVisible="False"
on them. It should disable them from all hit-testing