Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6326233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:03:04+00:00 2026-05-24T17:03:04+00:00

I want to add a contextmenu to items in a listbox. Typically the answer

  • 0

I want to add a contextmenu to items in a listbox. Typically the answer is to add the contextmenu to the root of the item template. However, I am using a template selector, so there are multiple templates in use, depending on the data of each item in the listbox. This means I need to add the same contextmenu definition to each template, which is not very appealing.

One solution is to wrap the datatemplate in a ContentControl, which would give me a single place for the context menu definition. However, I believe this would add layout overhead that isn’t necessary.

Another solution I tried is adding the ContextMenu to the resource dictionary, but I believe this ends up sharing the same object instance across all the uses, and due to the way ContextMenu is implemented, this also does not work.

A third solution is using the Loaded event to call a function which populates the context menu appropriately. However, this ends up moving a lot of code that ought to be in the XAML into code, and looks quite ugly. If there’s some way of defining the context menu in xaml, and then just referencing that from code, I would find it appealing, but I don’t quite see how to do that.

What is the right way to share the same ContextMenu across the templates in a template selector?


This is the ContentControl method, which works, but ends up adding two content controls to each item:

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <ContentControl>
                        <toolkit:ContextMenuService.ContextMenu>
                            <toolkit:ContextMenu Loaded="ContextMenu_Loaded">
                                <toolkit:MenuItem Header="Delete"/>
                            </toolkit:ContextMenu>
                        </toolkit:ContextMenuService.ContextMenu>
                        <ContentControl ContentTemplate="{StaticResource MyTemplate}" Content="{Binding}"/>
                    </ContentControl>
                </DataTemplate>
            </ListBox.ItemTemplate>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:03:05+00:00Added an answer on May 24, 2026 at 5:03 pm

    How about adding the ContextMenu to the TemplateSelector?

    <ListBox ItemsSource="{Binding}">
        <ListBox.Resources>
            <DataTemplate x:Key="MyTemplate">
                <StackPanel>
                    <TextBlock Text="{Binding}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.Resources>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <local:CustomTemplateSelector Content="{Binding}">
                    <toolkit:ContextMenuService.ContextMenu>
                        <toolkit:ContextMenu>
                            <toolkit:MenuItem Header="Delete"
                                                Click="MenuItem_Click" />
                        </toolkit:ContextMenu>
                    </toolkit:ContextMenuService.ContextMenu>
                    <local:CustomTemplateSelector.TemplateOne>
                        <DataTemplate>
                            <ContentControl Content="{Binding}"
                                            ContentTemplate="{StaticResource MyTemplate}"
                                            Foreground="Blue" />
                        </DataTemplate>
                    </local:CustomTemplateSelector.TemplateOne>
                    <local:CustomTemplateSelector.TemplateTwo>
                        <DataTemplate>
                            <ContentControl Content="{Binding}"
                                            ContentTemplate="{StaticResource MyTemplate}"
                                            Foreground="Red" />
                        </DataTemplate>
                    </local:CustomTemplateSelector.TemplateTwo>
                    <local:CustomTemplateSelector.TemplateThree>
                        <DataTemplate>
                            <ContentControl Content="{Binding}"
                                            ContentTemplate="{StaticResource MyTemplate}"
                                            Foreground="Yellow" />
                        </DataTemplate>
                    </local:CustomTemplateSelector.TemplateThree>
                </local:CustomTemplateSelector>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    I ran this and it worked for me – give it a try and let me know if this was the effect you were looking for or not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF contextmenu and I want to add an Item with Icon
I'm trying to add a ContextMenu to items in a ListBox in WPF; <ListBox.ItemTemplate>
I'm trying to sho a contextmenu on right-click on an item in a listbox.
I have a ContextMenu defined on a Datagrid but want to bind submenu items
I just want to add ContextMenu for several objects that I create dynamically, but
I am using the following method to add a ContextMenu to a custom view
I have dbml with single table users i want add partial class for User
I have a website built with ASP.NET (3.5) and want add some level of
I have a list of string values that I want add to a hashtable
I have numbers in javascript from 01(int) to 09(int) and I want add 1(int)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.