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 403189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:11:38+00:00 2026-05-12T17:11:38+00:00

Possible Duplicate: Specify Command for MenuItem in a DataTemplate I have a collection of

  • 0

Possible Duplicate:
Specify Command for MenuItem in a DataTemplate

I have a collection of objects (viewmodels) that represent menu items. Each of them have a command that I would like to execute when a MenuItem is clicked.

If I wanted to do the menu statically, I do it like this:

<ContextMenu>
    <MenuItem Header="{Binding Text1}" Command={Binding Command1}>
    <MenuItem Header="{Binding Text2}" Command={Binding Command2}>
</ContextMenu>

but when I don’t know the items in advance (they come from a collection), I need to assign ContextMenu.ItemsSource – and put a text into a ItemTemplate.

<ContextMenu ItemsSource="{Binding MyMenuItems}">
    <ContextMenu.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Text2}" /> <!-- But where to put Command binding? TextBlock.Command makes no sense, and we have no access to MenuItem! -->
        </DataTemplate>
    </ContextMenu.ItemTemplate>
</ContextMenu>

This way, however, I have no place to bind a Command to – because I can’t get the MenuItem for every row!

Any advice, please? Thank you, guys!

  • 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-12T17:11:39+00:00Added an answer on May 12, 2026 at 5:11 pm
    <ContextMenu.ItemContainerStyle>
      <Style TargetType="MenuItem">
        <Setter Property="Command" Value="{Binding AssociatedCommand}" />
      </Style>
    </ContextMenu.ItemContainerStyle>
    

    where AssociatedCommand is the property on the viewmodel object that holds the ICommand.

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

Sidebar

Related Questions

Possible Duplicate: Interface defining a constructor signature? I know that you cannot specify a
Possible Duplicate: Split into two variables? I have a variable that will output two
Possible Duplicate: how to specify a pointer to an overloaded function? I have a
Possible Duplicate: How do I specify the equivalent of volatile in VB.net? What is
Possible Duplicate: How can I specify the latest time of day with DateTime I
Possible Duplicate: When do I need to specify the JavaScript protocol? for example <body
Possible Duplicate: Mysql Offset Infinite rows Is it possible to specify a query in
Possible Duplicate: Creating a custom Document Library in SharePoint I have 2 users, Martin
Possible Duplicate: Including FSharp.Core in a C# project: resolving type collisions I have referenced
Possible Duplicate: Specifying formula in R with glm without explicit declaration of each covariate

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.