为什么在WPF MENU中自动打开子菜单?

时间:2017-02-09 07:03:01

标签: c# wpf xaml

我有一个菜单,其中包含少数项目的子菜单。第一次,当我点击菜单项时,子菜单会打开。在那之后,下一个子菜单在没有点击的情况下打开,只是悬停。我不想在没有点击的情况下打开其他子菜单。

    <ControlTemplate x:Key="MenuItemControlTemplate1" TargetType="{x:Type MenuItem}">
            <Border x:Name="templateRoot" 
                Background="{TemplateBinding Background}" 
                SnapsToDevicePixels="True" Margin="0,-7,0,5">
                <Grid VerticalAlignment="Center">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>


                    <ContentPresenter  x:Name="mainMenuHeaderContainer" Cursor="Hand" Margin="0,0,0,13" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Grid.Column="1" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header"  RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    <Popup x:Name="PART_Popup" AllowsTransparency="True" Focusable="False" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" Placement="Bottom">
                        <Border x:Name="SubMenuBorder" BorderBrush="#595959" BorderThickness=".5,0,0,0" Background="#32a3cf">
                            <ScrollViewer x:Name="SubMenuScrollViewer" 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="#666" Height="{Binding ActualHeight, ElementName=SubMenuBorder}" Width="{Binding ActualWidth, ElementName=SubMenuBorder}"/>
                                    </Canvas>
                                    <ItemsPresenter x:Name="ItemsPresenter"/>
                                </Grid>
                            </ScrollViewer>
                        </Border>
                    </Popup>
                </Grid>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsSuspendingPopupAnimation" Value="True">
                    <Setter Property="PopupAnimation" TargetName="PART_Popup" Value="None"/>
                </Trigger>
                <Trigger Property="IsHighlighted" Value="True">
                    <Setter Property="Background" TargetName="templateRoot" Value="#32a3cf"/>
                </Trigger>
                <Trigger Property="IsKeyboardFocusWithin" Value="True">
                    <Setter TargetName="templateRoot" Property="Background" Value="#32a3cf" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

        <ControlTemplate x:Key="MenuItemControlTemplate2" TargetType="{x:Type MenuItem}">
            <Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness=".5,0,.5,.5" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                <Grid Margin="-1" Cursor="Hand">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition MinWidth="22" SharedSizeGroup="MenuItemIconColumnGroup" Width="Auto"/>
                        <ColumnDefinition Width="13"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="30"/>
                        <ColumnDefinition SharedSizeGroup="MenuItemIGTColumnGroup" Width="Auto"/>
                        <ColumnDefinition Width="20"/>
                    </Grid.ColumnDefinitions>
                    <ContentPresenter x:Name="menuHeaderContainer" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Grid.Column="2" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" HorizontalAlignment="Left" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/>
                    <TextBlock x:Name="menuGestureText" Grid.Column="4" Margin="{TemplateBinding Padding}" Opacity="0.7" Text="{TemplateBinding InputGestureText}" VerticalAlignment="Center"/>
                </Grid>
            </Border>
            <ControlTemplate.Triggers>

                <Trigger Property="IsHighlighted" Value="True">
                    <Setter Property="BorderBrush" TargetName="templateRoot" Value="White"/>
                    <Setter Property="Background" TargetName="templateRoot" Value="#32a3cf"/>
                    <Setter Property="TextBlock.Foreground" TargetName="menuHeaderContainer" Value="White" />
                </Trigger>
                <!--<Trigger Property="IsEnabled" Value="False">
                    <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="#FF707070"/>
                </Trigger>-->
                <!--<MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsHighlighted" Value="True"/>
                        <Condition Property="IsEnabled" Value="False"/>
                    </MultiTrigger.Conditions>
                    <Setter Property="Background" TargetName="templateRoot" Value="#0A000000"/>
                    <Setter Property="BorderBrush" TargetName="templateRoot" Value="#21000000"/>
                </MultiTrigger>-->
            </ControlTemplate.Triggers>
        </ControlTemplate>


    <Menu x:Name="menu_main_project" Grid.Column="1" Grid.Row="0">

                        <MenuItem Height="42" Template="{DynamicResource MenuItemControlTemplate1}" Width="140">

                            <MenuItem.Header>
                                <DockPanel HorizontalAlignment="Stretch">
                                    <Rectangle x:Name="menuBarApplication" Fill="#32a3cf" Width="140" Height="4" DockPanel.Dock="Top" Margin="0,3,0,0"/>
                                    <Image Source="/Resources/ico_application.png" Stretch="Fill" DockPanel.Dock="Left" Margin="10,2,0,0" Height="19" Width="22">
                                    </Image>
                                    <ContentPresenter x:Name="APPLICATION_MENU" TextBlock.Foreground="#0e526a" Margin="4,0,0,0" Content="Application" DockPanel.Dock="Right" VerticalAlignment="Center"/>
                                </DockPanel>
                            </MenuItem.Header>
                            <!--<MenuItem Header="Learner"  x:Name="menuItem_LearnerApplicationHome" Click="OnClick_LearnerApplicationHome"/>-->
                            <MenuItem Template="{DynamicResource MenuItemControlTemplate2}" Header="DL" x:Name="menuItem_DLApplicationHome" Click="OnClick_DLApplicationHome" Foreground="#0e526a" Margin="-10,0,0,0" Background="White"/>
                            <MenuItem Template="{DynamicResource MenuItemControlTemplate2}" Header="Adjudication" x:Name="menuItem_Adjudication" Click="OnClick_AdjudicationHome" Foreground="#0e526a" Margin="-10,0,0,0" Background="White"/>
                            <MenuItem Template="{DynamicResource MenuItemControlTemplate2}" Header="Summary" x:Name="menuItem_Application_Summary" Click="OnClick_ApplicationSummaryHome" Foreground="#0e526a" Margin="-10,0,0,0" Background="White"/>
                        </MenuItem>
                        <MenuItem Width="105" Height="42" Template="{DynamicResource MenuItemControlTemplate1}">
                            <MenuItem.Header>
                                <DockPanel HorizontalAlignment="Stretch">
                                    <Rectangle x:Name="menuBarCard" Fill="#32a3cf" Width="105" Height="4" DockPanel.Dock="Top" Margin="0,3,0,0"/>
                                    <Image Source="/Resources/ico_card.png" Margin="20,2,0,0" Stretch="Fill" DockPanel.Dock="Left" Height="19" Width="22"/>
                                    <ContentPresenter x:Name="CARD_MENU" Margin="4,0,0,0" Content="Card" TextBlock.Foreground="#0e526a" DockPanel.Dock="Right" VerticalAlignment="Center"/>
                                </DockPanel>
                            </MenuItem.Header>
                            <MenuItem Header="Card" Template="{DynamicResource MenuItemControlTemplate2}"  Name="menuItem_CardHome" Click="OnClick_CardkHome" Margin="-10,0,0,0" Background="White" Foreground="#0e526a"/>
                            <MenuItem Header="Summary" Template="{DynamicResource MenuItemControlTemplate2}" x:Name="menuItem_Card_Summary" Click="OnClick_CardSummaryHome" Foreground="#0e526a" Margin="-10,0,0,0" Background="White"/>
<!-- Menu -->
                        </MenuItem>
                        <MenuItem Width="105" Height="42" Template="{DynamicResource MenuItemControlTemplate1}">
                            <MenuItem.Header>
                                <DockPanel HorizontalAlignment="Stretch">
                                    <Rectangle x:Name="menuBarStock" Fill="#32a3cf" Width="105" Height="4" DockPanel.Dock="Top" Margin="0,3,0,0"/>
                                    <Image Source="/Resources/ico_stock.png" Margin="18,2,0,0" Stretch="Fill" DockPanel.Dock="Left" Height="19" Width="22"/>
                                    <ContentPresenter x:Name="STOCK_MENU" Margin="4,0,0,0" Content="Stock" TextBlock.Foreground="#0e526a" DockPanel.Dock="Right" VerticalAlignment="Center"/>
                                </DockPanel>
                            </MenuItem.Header>
                            <MenuItem Header="Stock" Template="{DynamicResource MenuItemControlTemplate2}" Name="menuItem_StockHome" Click="OnClick_StockHome" Margin="-10,0,0,0" Background="White" Foreground="#0e526a"/>
                            <MenuItem Header="Item" Template="{DynamicResource MenuItemControlTemplate2}" Name="menuItem_ItemHome" Click="OnClick_ItemHome" Margin="-10,0,0,0" Background="White" Foreground="#0e526a"/>
                            <MenuItem Header="Summary" Template="{DynamicResource MenuItemControlTemplate2}" x:Name="menuItem_Stock_Summary" Click="OnClick_BoxSummaryHome" Foreground="#0e526a" Margin="-10,0,0,0" Background="White"/>

                        </MenuItem>
                        </Menu>

2 个答案:

答案 0 :(得分:0)

您可以使用IsMouseOver。以下是我遇到类似问题时所看到的内容。

Making 'IsMouseOver' open submenu items consistently

答案 1 :(得分:0)

这将是一个更好的解决方案,但这也将起作用

在xml上添加MenuItem属性

SubmenuOpened="MenuItem_SubmenuOpened" MouseEnter="MenuItem_MouseEnter"

并添加到您的代码

bool open = true;
    private void MenuItem_SubmenuOpened(object sender, RoutedEventArgs e)
    {
        ((MenuItem)sender).IsSubmenuOpen = open;
        open = true;
    }
    private void MenuItem_MouseEnter(object sender, MouseEventArgs e)
    {
        open = false;
    }

单击MenuItem时,会触发两次SubMenuOpened事件(一个用于悬停,一个用于单击)。 在悬停时,子菜单没有打开,因为那个bool。但是点击它会触发两次,第二次触发就会打开子菜单。

抱歉我的英语不好。