启用ContentPresenter之外的MenuItem突出显示

时间:2014-04-20 20:46:29

标签: c# wpf menu menuitem contentpresenter

我已经有一段时间了,它让我发疯了。这是我的问题...我有一个Menu,它使用默认模板,在突出显示,默认状态等情况下我改变了颜色。所以,没有必要在此处发布 - 它几乎完全相同(并且它是很多模板代码)。

我的问题是模板MenuItems属性创建的Margin之间的间距。它使用padding属性进行模板绑定,并在模板的(2, 3, 2, 3)周围生成ContentPresenter边距。

我想保留该财产,并需要将其分开;但是,当鼠标悬停在ContentPresenter之外时,鼠标悬停仅激活突出显示的状态。所以,"按钮"感觉不会被激活,直到我将鼠标移到单词本身上。 ContentPresenter没有填充,所以我不确定如何处理它。我尝试编辑模板附带的触发器以处理其他部分,但它不起作用(它们不会触发)。

模板附带的触发器仅适用于ContentPresenter。我尝试将其包装在ContentControl中以查看我是否可以获得Padding,但Padding的绑定不起作用,仅用于保证金...不确定为什么(它显示在属性中,但添加填充不可见):

<ContentControl SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                Padding="{TemplateBinding Padding}">
    <ContentPresenter ContentSource="Header" RecognizesAccessKey="True"/>
</ContentControl>

我也尝试将ContentPresenter完全替换为ContentControl,但再一次遭遇Padding无法正确绑定,手动Padding也没有做任何事情。或许有些东西会覆盖它。

我知道如何在那些MenuItems之间的空白处激活触发器吗?我处理它的方式是在我的MenuItem标题中添加空格......而这很荒谬。

这是我的模板:

<LinearGradientBrush x:Key="MenuItemSelectionFill" EndPoint="0,1" StartPoint="0,0">
    <GradientStop Color="#34C5EBFF" Offset="0"/>
    <GradientStop Color="#3481D8FF" Offset="1"/>
</LinearGradientBrush>

<Geometry x:Key="Checkmark">M 0,5.1 L 1.7,5.2 L 3.4,7.1 L 8,0.4 L 9.2,0 L 3.3,10.8 Z</Geometry>

<ControlTemplate x:Key="{ComponentResourceKey ResourceId=SubmenuItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}" TargetType="{x:Type MenuItem}">
    <Grid SnapsToDevicePixels="true" Background="#FFEFF6FB" HorizontalAlignment="Stretch">
        <Rectangle x:Name="Bg"/>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition MinWidth="24" SharedSizeGroup="MenuItemIconColumnGroup" Width="Auto"/>
                <ColumnDefinition Width="4"/>
                <ColumnDefinition/>
                <ColumnDefinition SharedSizeGroup="MenuItemIGTColumnGroup" Width="Auto"/>
                <ColumnDefinition Width="17"/>
            </Grid.ColumnDefinitions>
            <!--<ContentPresenter x:Name="Icon" VerticalAlignment="Center" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="1,12" ContentSource="Icon" Height="0"/>
            <Border x:Name="GlyphPanel" Width="Auto" Visibility="Hidden" Margin="1" Height="Auto" CornerRadius="3" Background="#FFEFF6FB" BorderThickness="1" BorderBrush="#FFEFF6FB">
                <Path x:Name="Glyph" Width="9" Height="11" FlowDirection="LeftToRight" Fill="#0C12A1" Data="{StaticResource Checkmark}"/>
            </Border>-->
            <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True" Margin="{TemplateBinding Padding}" ContentSource="Header" Grid.Column="2"/>
            <TextBlock Grid.Column="3" Margin="{TemplateBinding Padding}" Text="{TemplateBinding InputGestureText}"/>
        </Grid>
    </Grid>

    <ControlTemplate.Triggers>
        <!--<Trigger Property="Icon" Value="{x:Null}">
            <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
        </Trigger>-->
        <!--<Trigger Property="IsChecked" Value="true">
            <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/>
            <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
        </Trigger>-->
        <Trigger Property="IsHighlighted" Value="true">
            <Setter Property="Fill" TargetName="Bg" Value="#FFC0E5FF"/>
            <Setter Property="Foreground" Value="Black"/>
        </Trigger>
        <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Foreground" Value="#FF9A9A9A"/>
            <!--<Setter Property="Background" TargetName="GlyphPanel" Value="#EEE9E9"/>
            <Setter Property="BorderBrush" TargetName="GlyphPanel" Value="#DBD6D6"/>
            <Setter Property="Fill" TargetName="Glyph" Value="#848589"/>-->
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

<LinearGradientBrush x:Key="MenuItemPressedFill" EndPoint="0,1" StartPoint="0,0">
    <GradientStop Color="#28717070" Offset="0"/>
    <GradientStop Color="#50717070" Offset="0.75"/>
    <GradientStop Color="#90717070" Offset="1"/>
</LinearGradientBrush>

<SolidColorBrush x:Key="SubMenuBackgroundBrush" Color="#FFEFF6FB"/>

<Geometry x:Key="UpArrow">M 0,4 L 3.5,0 L 7,4 Z</Geometry>

<Style x:Key="MenuScrollButton" BasedOn="{x:Null}" TargetType="{x:Type RepeatButton}">
    <Setter Property="ClickMode" Value="Hover"/>
    <Setter Property="MinWidth" Value="0"/>
    <Setter Property="MinHeight" Value="0"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type RepeatButton}">
                <DockPanel Background="Transparent" SnapsToDevicePixels="true">
                    <Rectangle x:Name="R1" Width="1" Fill="Transparent" DockPanel.Dock="Right"/>
                    <Rectangle x:Name="B1" Height="1" Fill="Transparent" DockPanel.Dock="Bottom"/>
                    <Rectangle x:Name="L1" Width="1" Fill="Transparent" DockPanel.Dock="Left"/>
                    <Rectangle x:Name="T1" Height="1" Fill="Transparent" DockPanel.Dock="Top"/>
                    <ContentPresenter x:Name="ContentContainer" VerticalAlignment="Center" Margin="2,2,2,2" HorizontalAlignment="Center"/>
                </DockPanel>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="true">
                        <Setter Property="Fill" TargetName="R1" Value="{DynamicResource {x:Static SystemColors.ControlLightLightBrushKey}}"/>
                        <Setter Property="Fill" TargetName="B1" Value="{DynamicResource {x:Static SystemColors.ControlLightLightBrushKey}}"/>
                        <Setter Property="Fill" TargetName="L1" Value="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}"/>
                        <Setter Property="Fill" TargetName="T1" Value="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}"/>
                        <Setter Property="Margin" TargetName="ContentContainer" Value="3,3,1,1"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<MenuScrollingVisibilityConverter x:Key="MenuScrollingVisibilityConverter"/>

<Geometry x:Key="DownArrow">M 0,0 L 3.5,4 L 7,0 Z</Geometry>

<Style x:Key="{ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}" BasedOn="{x:Null}" TargetType="{x:Type ScrollViewer}">
    <Setter Property="HorizontalScrollBarVisibility" Value="Hidden"/>
    <Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ScrollViewer}">
                <Grid SnapsToDevicePixels="true">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Border Grid.Column="0" Grid.Row="1">
                        <ScrollContentPresenter CanContentScroll="{TemplateBinding CanContentScroll}" Margin="{TemplateBinding Padding}"/>
                    </Border>
                    <RepeatButton Style="{StaticResource MenuScrollButton}" Grid.Row="0" Focusable="false" Command="{x:Static ScrollBar.LineUpCommand}" CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Grid.Column="0">
                        <RepeatButton.Visibility>
                            <MultiBinding FallbackValue="Visibility.Collapsed" Converter="{StaticResource MenuScrollingVisibilityConverter}" ConverterParameter="0">
                                <Binding Path="ComputedVerticalScrollBarVisibility" RelativeSource="{RelativeSource TemplatedParent}"/>
                                <Binding Path="VerticalOffset" RelativeSource="{RelativeSource TemplatedParent}"/>
                                <Binding Path="ExtentHeight" RelativeSource="{RelativeSource TemplatedParent}"/>
                                <Binding Path="ViewportHeight" RelativeSource="{RelativeSource TemplatedParent}"/>
                            </MultiBinding>
                        </RepeatButton.Visibility>
                        <Path Data="{StaticResource UpArrow}" Fill="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}"/>
                    </RepeatButton>
                    <RepeatButton Style="{StaticResource MenuScrollButton}" Grid.Row="2" Focusable="false" Command="{x:Static ScrollBar.LineDownCommand}" CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Grid.Column="0">
                        <RepeatButton.Visibility>
                            <MultiBinding FallbackValue="Visibility.Collapsed" Converter="{StaticResource MenuScrollingVisibilityConverter}" ConverterParameter="100">
                                <Binding Path="ComputedVerticalScrollBarVisibility" RelativeSource="{RelativeSource TemplatedParent}"/>
                                <Binding Path="VerticalOffset" RelativeSource="{RelativeSource TemplatedParent}"/>
                                <Binding Path="ExtentHeight" RelativeSource="{RelativeSource TemplatedParent}"/>
                                <Binding Path="ViewportHeight" RelativeSource="{RelativeSource TemplatedParent}"/>
                            </MultiBinding>
                        </RepeatButton.Visibility>
                        <Path Data="{StaticResource DownArrow}" Fill="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}"/>
                    </RepeatButton>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<ControlTemplate x:Key="{ComponentResourceKey ResourceId=TopLevelHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}" TargetType="{x:Type MenuItem}">
    <Grid SnapsToDevicePixels="true">
        <Rectangle x:Name="OuterBorder"/>
        <Rectangle x:Name="Bg"/>
        <DockPanel>
            <!--<ContentPresenter x:Name="Icon" ContentSource="Icon" Margin="0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/>
            <Path x:Name="GlyphPanel" Data="{StaticResource Checkmark}" Fill="{TemplateBinding Foreground}" FlowDirection="LeftToRight" Margin="0" Visibility="Collapsed" VerticalAlignment="Center"/>-->
            <ContentPresenter ContentSource="Header" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="0,1"/>
        </DockPanel>
        <Popup x:Name="PART_Popup" AllowsTransparency="true" Focusable="false" HorizontalOffset="0" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" Placement="Bottom" VerticalOffset="0">
            <Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent">
                <Border x:Name="SubMenuBorder" Background="{StaticResource SubMenuBackgroundBrush}">
                    <ScrollViewer x:Name="SubMenuScrollViewer" Margin="1,0" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}">
                        <Grid RenderOptions.ClearTypeHint="Enabled">
                            <Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
                                <Rectangle x:Name="OpaqueRect" Fill="{StaticResource SubMenuBackgroundBrush}" Height="{Binding ActualHeight, ElementName=SubMenuBorder}" Width="{Binding ActualWidth, ElementName=SubMenuBorder}"/>
                            </Canvas>
                            <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="true" Margin="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.TabNavigation="Cycle"/>
                        </Grid>
                    </ScrollViewer>
                </Border>
            </Themes:SystemDropShadowChrome>
        </Popup>
    </Grid>

    <ControlTemplate.Triggers>
        <Trigger Property="IsSuspendingPopupAnimation" Value="true">
            <Setter Property="PopupAnimation" TargetName="PART_Popup" Value="None"/>
        </Trigger>
        <!--<Trigger Property="Icon" Value="{x:Null}">
            <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
        </Trigger>
        <Trigger Property="IsChecked" Value="true">
            <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/>
            <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
        </Trigger>-->
        <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="true">
            <Setter Property="Margin" TargetName="Shdw" Value="0,0,5,5"/>
            <Setter Property="Color" TargetName="Shdw" Value="#71000000"/>
        </Trigger>
        <Trigger Property="IsHighlighted" Value="true">
            <Setter Property="Fill" TargetName="Bg" Value="#FFEFF6FB"/>
            <Setter Property="Foreground" Value="Black"/>
            <!--<Setter Property="Stroke" TargetName="Bg" Value="#90717070"/>-->
            <!--<Setter Property="Stroke" TargetName="OuterBorder" Value="#50FFFFFF"/>-->
            <!--<Setter Property="Stroke" TargetName="InnerBorder" Value="#50FFFFFF"/>-->
        </Trigger>
        <Trigger Property="IsKeyboardFocused" Value="true">
            <!--<Setter Property="Stroke" TargetName="Bg" Value="#E0717070"/>-->
            <Setter Property="Fill" TargetName="Bg" Value="#FFEFF6FB"/>
            <Setter Property="Foreground" Value="Black"/>
            <!--<Setter Property="Stroke" TargetName="InnerBorder" Value="#50747272"/>-->
        </Trigger>
        <Trigger Property="IsSubmenuOpen" Value="true">
            <Setter Property="Fill" TargetName="Bg" Value="#FFEFF6FB"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="Fill" TargetName="OuterBorder" Value="{x:Null}"/>
            <Setter Property="Stroke" TargetName="OuterBorder" Value="{x:Null}"/>
        </Trigger>
        <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Foreground" Value="#FF9A9A9A"/>
            <!--<Setter Property="Fill" TargetName="GlyphPanel" Value="#848589"/>-->
        </Trigger>
        <Trigger Property="ScrollViewer.CanContentScroll" SourceName="SubMenuScrollViewer" Value="false">
            <Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=SubMenuScrollViewer}"/>
            <Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=SubMenuScrollViewer}"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

<ControlTemplate x:Key="{ComponentResourceKey ResourceId=TopLevelItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}" TargetType="{x:Type MenuItem}">
    <Grid SnapsToDevicePixels="true">
        <Rectangle x:Name="Bg"/>
        <DockPanel>
            <ContentPresenter x:Name="Icon" ContentSource="Icon" Margin="4,0,6,0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/>
            <Path x:Name="GlyphPanel" Data="{StaticResource Checkmark}" Fill="{TemplateBinding Foreground}" FlowDirection="LeftToRight" Margin="7,0,0,0" Visibility="Collapsed" VerticalAlignment="Center"/>
            <ContentPresenter ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
        </DockPanel>
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="Icon" Value="{x:Null}">
            <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
        </Trigger>
        <Trigger Property="IsChecked" Value="true">
            <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/>
            <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
        </Trigger>
        <Trigger Property="IsHighlighted" Value="true">
            <Setter Property="Fill" TargetName="Bg" Value="#FFEFF6FB"/>
            <Setter Property="Foreground" Value="Black"/>
        </Trigger>
        <Trigger Property="IsKeyboardFocused" Value="true">
            <Setter Property="Fill" TargetName="Bg" Value="#FFEFF6FB"/>
            <Setter Property="Foreground" Value="Black"/>
            <!--<Setter Property="Fill" TargetName="Bg" Value="{StaticResource MenuItemPressedFill}"/>-->
        </Trigger>
        <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Foreground" Value="#FF9A9A9A"/>
            <Setter Property="Fill" TargetName="GlyphPanel" Value="#848589"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

<Geometry x:Key="RightArrow">M 0,0 L 4,3.5 L 0,7 Z</Geometry>

<ControlTemplate x:Key="{ComponentResourceKey ResourceId=SubmenuHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}" TargetType="{x:Type MenuItem}">
    <Grid SnapsToDevicePixels="true">
        <Rectangle x:Name="Bg"/>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition MinWidth="24" SharedSizeGroup="MenuItemIconColumnGroup" Width="Auto"/>
                <ColumnDefinition Width="4"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="37"/>
                <ColumnDefinition SharedSizeGroup="MenuItemIGTColumnGroup" Width="Auto"/>
                <ColumnDefinition Width="17"/>
            </Grid.ColumnDefinitions>
            <ContentPresenter x:Name="Icon" ContentSource="Icon" Margin="1" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/>
            <Border x:Name="GlyphPanel" BorderBrush="#CDD3E6" BorderThickness="1" Background="#E6EFF4" CornerRadius="3" Height="22" Margin="1" Visibility="Hidden" Width="22">
                <Path x:Name="Glyph" Data="{StaticResource Checkmark}" Fill="#0C12A1" FlowDirection="LeftToRight" Height="11" Width="9"/>
            </Border>
            <ContentPresenter Grid.Column="2" ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
            <TextBlock Grid.Column="4" Margin="{TemplateBinding Padding}" Text="{TemplateBinding InputGestureText}" Visibility="Collapsed"/>
            <Path Grid.Column="5" Data="{StaticResource RightArrow}" Fill="{TemplateBinding Foreground}" Margin="4,0,0,0" VerticalAlignment="Center"/>
        </Grid>
        <Popup x:Name="PART_Popup" AllowsTransparency="true" Focusable="false" HorizontalOffset="-2" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" Placement="Right" VerticalOffset="-3">
            <Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent">
                <Border x:Name="SubMenuBorder">
                    <ScrollViewer x:Name="SubMenuScrollViewer" Margin="1,0" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}">
                        <Grid RenderOptions.ClearTypeHint="Enabled">
                            <Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
                                <Rectangle x:Name="OpaqueRect" Fill="{StaticResource SubMenuBackgroundBrush}" Height="{Binding ActualHeight, ElementName=SubMenuBorder}" Width="{Binding ActualWidth, ElementName=SubMenuBorder}"/>
                            </Canvas>
                            <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="true" Margin="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.TabNavigation="Cycle"/>
                        </Grid>
                    </ScrollViewer>
                </Border>
            </Themes:SystemDropShadowChrome>
        </Popup>
    </Grid>

    <ControlTemplate.Triggers>
        <Trigger Property="IsSuspendingPopupAnimation" Value="true">
            <Setter Property="PopupAnimation" TargetName="PART_Popup" Value="None"/>
        </Trigger>
        <Trigger Property="IsHighlighted" Value="true">
            <Setter Property="Fill" TargetName="Bg" Value="#FFEFF6FB"/>
            <Setter Property="Foreground" Value="Black"/>
        </Trigger>
        <Trigger Property="Icon" Value="{x:Null}">
            <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
        </Trigger>
        <Trigger Property="IsChecked" Value="true">
            <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/>
            <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
        </Trigger>
        <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="true">
            <Setter Property="Margin" TargetName="Shdw" Value="0,0,5,5"/>
            <Setter Property="Color" TargetName="Shdw" Value="#71000000"/>
        </Trigger>
        <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Foreground" Value="#FF9A9A9A"/>
            <Setter Property="Background" TargetName="GlyphPanel" Value="#EEE9E9"/>
            <Setter Property="BorderBrush" TargetName="GlyphPanel" Value="#DBD6D6"/>
            <Setter Property="Fill" TargetName="Glyph" Value="#848589"/>
        </Trigger>
        <Trigger Property="ScrollViewer.CanContentScroll" SourceName="SubMenuScrollViewer" Value="false">
            <Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=SubMenuScrollViewer}"/>
            <Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=SubMenuScrollViewer}"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

1 个答案:

答案 0 :(得分:1)

问题在于MenuItem模板,其中包含ResourceID&#34; TopLevelItemTemplateKey &#34;。

请注意,在此模板中,没有为菜单项的透明区域设置背景。从技术上讲,这些领域的背景是不透明的,它是 null - 没有为许多UI元素设置背景,让鼠标命中测试在没有内容的区域失败(命中测试没有完成像素逐像素,而不是特定的 UIElement.InputHitTest 方法实现。)

将背景明确设置为&#34;透明&#34;在模板中最外层的UI元素上将解决此问题:

<ControlTemplate
    x:Key="{ComponentResourceKey ResourceId=TopLevelItemTemplateKey,
           TypeInTargetAssembly={x:Type MenuItem}}"
    TargetType="{x:Type MenuItem}"
>

    <!-- ### FIX: SETTING THE GRID BACKGROUND TO "TRANSPARENT" ### -->

    <Grid Background="Transparent" SnapsToDevicePixels="true">
        <Rectangle x:Name="Bg"/>
        ...
    </Grid>
</ControlTemplate>

(如果您想了解有关WPF中命中测试的更多信息,请read here in the MSDN documentation。)