Question

I have created a custom context menu in a separate user Control Class CustomContextMenuUc.

The simple version of the code looks like this.

<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>

I want to use this CustomContextMenuUc in multiple Windows. I am able to assign this User control in C# code like

ContextMenuGrid.ContextMenu = new CustomContextMenuUc();

Where ContextMenuGrid is a grid.

I want to do this in XAML How should I do it?

<DataGrid ContextMenu="">
Was it helpful?

Solution

You will have to define the Resource in resources of your window like

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

here local is the xmlns where your contextmenu is defined.

Then you can do

<DataGrid ContextMenu="{StaticResource MyContextMenu}">

Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top