wpf contextmenu点击打开子菜单

时间:2016-07-26 21:11:32

标签: .net wpf xaml contextmenu menuitem

我在WPF中有一个带有多个子菜单的上下文菜单。默认行为是,如果将鼠标悬停在菜单项下方的项目上,则会打开子菜单。但是,我想改变它,以便它在悬停时不会打开,但是在左键单击时。

我制作了一个MenuItem默认模板的副本。在这,我做了一些改变:

<ControlTemplate x:Key="{ComponentResourceKey ResourceId=SubmenuHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}" TargetType="{x:Type MenuItem}">
    <!-- some default template code -->
    <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="IsHighlighted" Value="True">
            <Setter Property="Background" TargetName="templateRoot" Value="Transparent"/>
            <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Border}"/>
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="{StaticResource Menu.Disabled.Foreground}"/>
            <Setter Property="Fill" TargetName="Glyph" Value="{StaticResource Menu.Disabled.Foreground}"/>
            <Setter Property="Fill" TargetName="RightArrow" Value="{StaticResource Menu.Disabled.Foreground}"/>
        </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>

        <!-- my code to supress the opening of the submenu in mouseover -->
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="IsOpen" TargetName="PART_Popup" Value="False"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

这似乎最初起作用。然而,当我慢慢地将鼠标移动到另一个menuItem时,它会出现片刻 - 可能是“IsMouseOver”的一些空间。不是真的,但子菜单仍然显示。

我怎样才能完全抑制它 - 除非有人点击menuItem?

1 个答案:

答案 0 :(得分:0)

此行为是由TemplateBinding IsSubmenuOpen引起的,IsOpen会在触发器将其设置为False之前自动将<Popup Name="PART_Popup" Placement="Right" HorizontalOffset="-4" IsOpen="{TemplateBinding IsSubmenuOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Fade"> <Border ... </Border> </Popup> 设置为True。

IsOpen="False"

由于您已经使用Click事件连接了所有内容,因此您只需设置

即可
IsOpen="{TemplateBinding IsChecked}"

解决这个问题的另一种方法是设置

public function handle(){ //fire for Laravel 4.*

    $tables = explode(',', $this->option('tables'));//default []
    $skip = explode(',', $this->option('skip'));//default [] 
    $migrations = glob("*table*.php");//get all migrations
    foreach($migrations as $migrate){
        //if tables argument is set, check to see if part of tables
        //if file name not like any in skip.. you get the point

每次MenuItem单击时设置IsChecked属性。