简体   繁体   中英

How to use a ContextMenu UserControl in 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:

在此输入图像描述

And code-behind:

在此输入图像描述

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

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?

Try to defineit like:

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

and after use it like

<DataGrid ContextMenu="{StaticResource DgContextMenu}

Should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM