Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为网格设置ContextMenu用户控件_C#_Wpf_Xaml_User Controls_Custom Contextmenu - Fatal编程技术网

C# 为网格设置ContextMenu用户控件

C# 为网格设置ContextMenu用户控件,c#,wpf,xaml,user-controls,custom-contextmenu,C#,Wpf,Xaml,User Controls,Custom Contextmenu,我在一个单独的用户控件类CustomContextMenuUc中创建了一个自定义上下文菜单 代码的简单版本如下所示 <ContextMenu x:Class="CustomContextMenu.CustomContextMenuUc" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.co

我在一个单独的用户控件类CustomContextMenuUc中创建了一个自定义上下文菜单

代码的简单版本如下所示

<ContextMenu x:Class="CustomContextMenu.CustomContextMenuUc"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            ItemsSource="{Binding Groups}">
    <ContextMenu.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Items}">
            <TextBlock Text="{Binding Description.Value}" />
        </HierarchicalDataTemplate>
    </ContextMenu.ItemTemplate>
    <ContextMenu.OverridesDefaultStyle>True</ContextMenu.OverridesDefaultStyle>
    <ContextMenu.Placement>Custom</ContextMenu.Placement>
</ContextMenu>
其中
ContextMenuGrid
是一个网格

我想在XAML中这样做,我应该怎么做

<DataGrid ContextMenu="">

您必须在窗口的资源中定义资源,如

<local:CustomContextMenuUc x:Key="MyContextMenu"/>
谢谢

<local:CustomContextMenuUc x:Key="MyContextMenu"/>
<DataGrid ContextMenu="{StaticResource MyContextMenu}">