Context Menu on a menu item

Ribbon for WPF Forum

Posted 9 years ago by sbarry
Version: 10.2.0531
Platform: .NET 3.5
Environment: Windows 7 (64-bit)
Avatar
Hello,

I have a ribbon drop down that contains items that the user can delete/edit. I would like to have a context menu that is available on each item of the drop down with "delete" and "edit" menu items. The problem is that the menu items are not highlighting when the cursor is placed over the context menu's items.

Below is the xaml and codebehind that is used to reproduce the issue. Any ideas?

<ribbon:RibbonWindow x:Class="RibbonDropdownRightClickMenu.Window1"
        xmlns:ribbon="http://schemas.actiprosoftware.com/winfx/xaml/ribbon"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" >
    <DockPanel>
        <ribbon:Ribbon DockPanel.Dock="Top">
            <ribbon:Tab Label="Home">
                <ribbon:Group Label="Test">
                    <ribbon:PopupButton Label="Drop Down" >
                        <StackPanel>
                            <ribbon:Menu>
                                <ribbon:Separator Label="Menu Header" />
                            </ribbon:Menu>

                            <ribbon:Menu ItemsSource="{Binding ItemsInMenu}">
                                <ribbon:Menu.ItemTemplate>
                                    <DataTemplate DataType="ribbon:SimpleDataItem">
                                        <ribbon:Button Context="MenuItem" Label="{Binding Name}" >
                                            <ribbon:Button.ContextMenu>
                                                <ribbon:ContextMenu>
                                                    <ribbon:Button Context="MenuItem" Label="Delete" />
                                                    <ribbon:Button Context="MenuItem" Label="Edit" />
                                                </ribbon:ContextMenu>
                                            </ribbon:Button.ContextMenu>
                                        </ribbon:Button>                                        
                                    </DataTemplate>
                                </ribbon:Menu.ItemTemplate>
                                
                            </ribbon:Menu>                                                 
                        </StackPanel>
                    </ribbon:PopupButton>
                </ribbon:Group>
                
            </ribbon:Tab>
        </ribbon:Ribbon>
    </DockPanel>
</ribbon:RibbonWindow>

using System.Collections.ObjectModel;
using System.ComponentModel;
using ActiproSoftware.Windows.Controls.Ribbon;

namespace RibbonDropdownRightClickMenu
{
    public class ViewModelBase : INotifyPropertyChanged
    {
       public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void NotifyPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, args);
            }
        }
    }

    public class MenuItemViewModel : ViewModelBase
    {
        private string _name;

        public string Name
        {
            get { return _name; }
            set
            {
                if (_name != value)
                {
                    _name = value;
                    NotifyPropertyChanged("Name");
                }
            }
        }
    }

    public class RibbonViewModel
    {
        ObservableCollection<MenuItemViewModel> _itemsInMenu = new ObservableCollection<MenuItemViewModel>();

        public ObservableCollection<MenuItemViewModel> ItemsInMenu { get { return _itemsInMenu; } }

        public RibbonViewModel()
        {
            _itemsInMenu.Add(new MenuItemViewModel() { Name = "First" });
            _itemsInMenu.Add(new MenuItemViewModel() { Name = "First" });
            _itemsInMenu.Add(new MenuItemViewModel() { Name = "First" });
            _itemsInMenu.Add(new MenuItemViewModel() { Name = "First" });
        }
    }
    
    public partial class Window1 : RibbonWindow
    {
        public Window1()
        {
            DataContext = new RibbonViewModel();

            InitializeComponent();
        }
    }
}

Comments (6)

Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Unfortunately I don't think you'll be able to do a context menu on a menu item. Maybe instead you could make your main menu item a SplitButton and put the Edit/Delete in the popup menu for that.


Actipro Software Support

Posted 9 years ago by sbarry
Avatar
Could you elaborate on this please? Standard WPF controls allow me to do it just fine.
Posted 9 years ago by sbarry
Avatar
I'm not sure what you mean by using a split button. Can you be more descriptive please?
Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi, sorry but we were researching the problem, trying to get more info on why it wasn't showing highlights.

It turns out that due to our wanting to make menus really flexible for layouts, we need have you wrap ribbon:Button "menu items" in a ribbon:Menu. That way you can stack multiple ribbon:Menu's side-by-side or inject other random static content that is not a menu item around them.

So if you do this it works:
<ribbon:ContextMenu>
    <ribbon:Menu>
        <ribbon:Button Label="Delete" />
        <ribbon:Button Label="Edit" />
    </ribbon:Menu>
</ribbon:ContextMenu>


Actipro Software Support

Posted 9 years ago by sbarry
Avatar
That worked! Thanks!
Posted 9 years ago by sbarry
Avatar
That worked! Thanks!
The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.