确保箭头键跳过 WPF 菜单中的分隔符项目?

发布于 2024-12-15 10:33:11 字数 1461 浏览 3 评论 0原文

在我的 WPF 菜单中,我有一个分隔符 (System.Windows.Controls.Separator) 项,当通过箭头键导航菜单时,它似乎无形地接受焦点。我希望箭头导航路径能够跳过这些分隔符。我尝试将 IsEnabledIsTabStop 设置为 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 技术交流群。

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

发布评论

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

评论(2

七七 2024-12-22 10:33:11

我想通了。我需要在 MenuItem 本身而不是模板上设置 Focusable:

<!-- style for menu item separators -->
            <Style TargetType="{x:Type MenuItem}" 
           BasedOn="{StaticResource {x:Type MenuItem}}" 
           x:Key="MenuItemStyleSeparator">
                            <!-- the following line is what I needed -->
                <Setter Property="Focusable" Value="False" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type MenuItem}">
                            <Separator IsEnabled="false" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

I figured it out. I needed to set Focusable on the MenuItem itself, not the template:

<!-- style for menu item separators -->
            <Style TargetType="{x:Type MenuItem}" 
           BasedOn="{StaticResource {x:Type MenuItem}}" 
           x:Key="MenuItemStyleSeparator">
                            <!-- the following line is what I needed -->
                <Setter Property="Focusable" Value="False" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type MenuItem}">
                            <Separator IsEnabled="false" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
┈┾☆殇 2024-12-22 10:33:11

尝试对其设置 IsHitTestVisible="False" 。它应该禁止它们进行所有命中测试

Try setting IsHitTestVisible="False" on them. It should disable them from all hit-testing

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