I use MVVM to bind views to objects in a tree. I have a base class that implements elements in my tree, and this base class has the ContextMenu property:
public IEnumerable<IMenuItem> ContextMenu { get { return m_ContextMenu; } protected set { if (m_ContextMenu != value) { m_ContextMenu = value; NotifyPropertyChanged(m_ContextMenuArgs); } } } private IEnumerable<IMenuItem> m_ContextMenu = null; static readonly PropertyChangedEventArgs m_ContextMenuArgs = NotifyPropertyChangedHelper.CreateArgs<AbstractSolutionItem>(o => o.ContextMenu);
A view that binds to the base class (and all derived classes) implements ContextMenu, which binds to this property:
<ContextMenu x:Name="contextMenu" ItemsSource="{Binding Path=(local:AbstractSolutionItem.ContextMenu)}" IsEnabled="{Binding Path=(local:AbstractSolutionItem.ContextMenuEnabled)}" ItemContainerStyle="{StaticResource contextMenuStyle}"/>
Each menu item is attached to an IMenuItem object (ViewModel for menu items). When you click on a menu item, it uses commands to execute the command on the base object. All of this works great.
However, as soon as the command is executed in the IMenuItem class, sometimes you need to get a link to the object that the user right-clicked on to bring up the context menu (or ViewModel of this object, at least). This is the whole point of the context menu. How do I apply for passing a link to a ViewModel element of a tree in a MenuItem ViewModel? Note that some context menus are shared by many objects in the tree.
c # wpf mvvm contextmenu
Scott whitlock
source share