kelevrax
kelevrax

Reputation: 51

Can I open a submenu menuitem on click and not on mouse over in c# (WPF)?

I have a menu made of 3 level of menuitem, the first level open its submenu only on click, the second submenu level open its submenu when the mouse is over, i would change this behaviour, to obtain a submenu that only open its submenu on click

Upvotes: 4

Views: 1880

Answers (2)

AnjumSKhan
AnjumSKhan

Reputation: 9827

  • Opening / Closing of a submenu is governed by MenuItem.IsSubmenuOpen property.

  • All we need is to change the value of MenuItem.IsSubmenuOpen accordingly in such a way that it should not get overridden.

  • We know that Coercion has highest precedence.

  • To Coerce its value, we need to provide a CoerceValueCallback, and for that we need to create a new MenuItem control, and a new Menu to use our new MenuItem.

MenuOpenOnlyOnClick.xaml

    <Menu
        x:Class=" WpfStackOverflow.Controls.MenuOpenOnlyOnClick" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"              
        mc:Ignorable="d" 
        d:DesignHeight="300" d:DesignWidth="300">   
    </Menu>

MenuOpenOnlyOnClick.xaml.cs

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Linq;

namespace WpfStackOverflow.Controls
{
    /// <summary>
    /// Interaction logic for MenuOpenOnlyOnClick.xaml
    /// </summary>
    public partial class MenuOpenOnlyOnClick : Menu
    {
        public MenuOpenOnlyOnClick()
        {
            InitializeComponent();            
        }

        protected override DependencyObject GetContainerForItemOverride()
        {
            return new MenuItemNew();
        }
    }

    public class MenuItemNew : MenuItem
    {
        #region Constructors
        static MenuItemNew()
        {
            MenuItem.IsSubmenuOpenProperty.OverrideMetadata(typeof(MenuItemNew),
                new FrameworkPropertyMetadata(false, MenuItem.IsSubmenuOpenProperty.DefaultMetadata.PropertyChangedCallback, CoerceIsSubmenuOpen));
        }

        public MenuItemNew()
        {
            this.PreviewMouseLeftButtonDown += MenuItemNew_PreviewMouseLeftButtonDown;
            this.LostFocus += MenuItemNew_LostFocus;
        }
        #endregion

        #region Event Handlers
        void MenuItemNew_LostFocus(object sender, RoutedEventArgs e)
        {
            MenuItemNew item = sender as MenuItemNew;
            item.IsMenuItemExpanded = false;
        }

        void MenuItemNew_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MenuItemNew item = sender as MenuItemNew;

            if (item.Role == MenuItemRole.SubmenuHeader)
            {
                IsMenuItemExpanded = !IsMenuItemExpanded;
                IsSubmenuOpen = IsMenuItemExpanded;
            }
        }
        #endregion

        #region IsMenuItemExpanded Dependency Property

        public bool IsMenuItemExpanded
        {
            get { return (bool)GetValue(IsExpanderClickedProperty); }
            set { SetValue(IsExpanderClickedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsMenuItemExpanded.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsExpanderClickedProperty =
            DependencyProperty.Register("IsMenuItemExpanded", typeof(bool), typeof(MenuItemNew),
            new PropertyMetadata(false, null));

        #endregion

        #region Overrides
        protected override DependencyObject GetContainerForItemOverride()
        {
            return new MenuItemNew();
        }
        #endregion

        #region Dependency Property Callbacks
        private static object CoerceIsSubmenuOpen(DependencyObject d, object value)
        {
            if ((bool)value)
            {
                MenuItemNew item = (MenuItemNew)d;

                if (item.Role == MenuItemRole.SubmenuHeader)
                {
                    if (item.IsMenuItemExpanded)
                        return true;
                    else
                        return false;
                }                

                if (!item.IsLoaded)
                {
                    item.Loaded += (s, e) =>
                    {
                        Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Input, new DispatcherOperationCallback(delegate(object param)
                        {
                            item.CoerceValue(MenuItemNew.IsSubmenuOpenProperty);
                            return null;
                        }), null);
                    };
                    return false;
                }
            }
            return value;
        }
        #endregion  
    }
}

Usage

<Window x:Class="WpfStackOverflow.Window17"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ctrl="clr-namespace:WpfStackOverflow.Controls"
        Title="Window17" Height="300" Width="300">

    <Grid>
        <ctrl:MenuOpenOnlyOnClick VerticalAlignment="Top" >
            <ctrl:MenuItemNew Header="File">
                <ctrl:MenuItemNew Header="New">
                    <ctrl:MenuItemNew Header="Project"/>
                    <ctrl:MenuItemNew Header="Website"/>
                </ctrl:MenuItemNew>
                <ctrl:MenuItemNew Header="Open"/>
            </ctrl:MenuItemNew>
        </ctrl:MenuOpenOnlyOnClick>
    </Grid>

</Window>

Upvotes: 1

King King
King King

Reputation: 63377

I'm not sure if a better solution exists. You can try this code to suppress the submenu from opening on hovering and open it on clicking:

//Suppose menu is the name of your Menu (or ContextMenu)
bool suppressOpen = true;
menu.AddHandler(MenuItem.SubmenuOpenedEvent, new RoutedEventHandler((s, e) =>
{
   if (suppressOpen) ((MenuItem)e.Source).IsSubmenuOpen = false;
   else suppressOpen = true;
}));
menu.AddHandler(MenuItem.PreviewMouseDownEvent, 
                new MouseButtonEventHandler((s, e) =>
{
   suppressOpen = false;
   ((MenuItem)e.Source).IsSubmenuOpen = true;
}));

Upvotes: 0

Related Questions