简体   繁体   English

如何在WPF中使用ContextMenu UserControl?

[英]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 . 不幸的是,这不起作用,因为指定的值不正确并且预期是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 此外,我需要能够收听OnContextMenuOpening事件,因为打开时的菜单需要对菜单做一些工作,并且不会为上下文菜单触发事件: http//connect.microsoft.com/VisualStudio/反馈/信息/ 353112 /文本菜单打开-事件犯规火,正确

"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." “ContextMenu本身是一个FrameworkElement派生类,但不会从作为源打开的上下文菜单中引发此事件。该事件是从”拥有“上下文菜单作为属性的元素引发的,仅在用户引发时才会引发尝试在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 . 但我得到: ContextMenu cannot have a logical or visual parent

Regardless of how you call your UserControl, it is not a ContextMenu. 无论您如何调用UserControl,它都不是ContextMenu。 You would have to derive from ContextMenu instead of UserControl: 您必须从ContextMenu而不是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. 应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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