【问题标题】:ensure that arrow keys skip separator items in a WPF menu?确保箭头键跳过 WPF 菜单中的分隔项?
【发布时间】:2011-11-15 19:43:54
【问题描述】:

在我的 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>

【问题讨论】:

    标签: wpf separator


    【解决方案1】:

    我想通了。我需要在 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>
    

    【讨论】:

      【解决方案2】:

      尝试在它们上设置IsHitTestVisible="False"。它应该从所有命中测试中禁用它们

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-02-27
        • 2014-03-20
        • 1970-01-01
        • 1970-01-01
        • 2011-08-08
        • 1970-01-01
        • 2014-06-21
        相关资源
        最近更新 更多