Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a ContextMenu UserControl in WPF?

Tags:

c#

mvvm

wpf

I have a user control like this:

<UserControl x:Class="MyApp.UserControls.MyContextMenu"
             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"
             ContextMenuOpening="OnContextMenuOpening"
             d:DesignHeight="300" d:DesignWidth="300">

    <UserControl.ContextMenu>
        <ContextMenu>
        ...
        </ContextMenu>
    </UserControl.ContextMenu>
</UserControl>

My question is: how do I use that context menu for something like a data grid:

<DataGrid ContextMenu="{usercontrols:MyContextMenu}"

Unfortunately that does not work because the specified value is incorrect and expected a ContextMenu.

Note: I need to reuse my context menu in several places, so I have put it in its own file. Also, I need to be able to listen to OnContextMenuOpening events, because the menu upon opening needs to do some work regarding the menu and the event is not fired for the context menu sadly: http://connect.microsoft.com/VisualStudio/feedback/details/353112/contextmenu-opening-event-doesnt-fire-properly

"ContextMenu itself is a FrameworkElement derived class, but this event will not be raised from the context menu being opened as a source. The event is raised from the element that "owns" the context menu as a property and is only raised when a user attempts to open a context menu in the UI."

This event problem is the reason I have put the menu for a user control -- so that the user control can get the event and do the work.

Update: I tried to have it as a root element and extend the context menu:

enter image description here

And code-behind:

enter image description here

But I'm getting: ContextMenu cannot have a logical or visual parent.

like image 678
Tower Avatar asked May 17 '12 12:05

Tower


People also ask

How to use ContextMenu in WPF?

A ContextMenu is attached to a specific control. The ContextMenu element enables you to present users with a list of items that specify commands or options that are associated with a particular control, for example, a Button. Users right-click the control to make the menu appear.

What is UserControl WPF?

User controls, in WPF represented by the UserControl class, is the concept of grouping markup and code into a reusable container, so that the same interface, with the same functionality, can be used in several different places and even across several applications.

What is the difference between UserControl and window in WPF?

A window is managed by the OS and is placed on the desktop. A UserControl is managed by wpf and is placed in a Window or in another UserControl. Applcations could be created by have a single Window and displaying lots of UserControls in that Window.


2 Answers

Regardless of how you call your UserControl, it is not a ContextMenu. You would have to derive from ContextMenu instead of UserControl:

<ContextMenu x:Class="MyApp.MyContextMenu"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <MenuItem Header="Item 1"/>
    <MenuItem Header="Item 2"/>
    ...
</ContextMenu>

and

public partial class MyContextMenu : ContextMenu
{
    public MyContextMenu()
    {
        InitializeComponent();
    }
}

But why would you do that at all?

like image 121
Clemens Avatar answered Sep 23 '22 09:09

Clemens


Try to defineit like:

<DataGrid.Resources>
    <ContextMenu x:Key="DgContextMenu">
      ...
    </ContextMenu>
</DataGrid.Resources>

and after use it like

<DataGrid ContextMenu="{StaticResource DgContextMenu}

Should work.

like image 45
Tigran Avatar answered Sep 21 '22 09:09

Tigran