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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:49:41+00:00 2026-05-18T22:49:41+00:00

i wanted a contextmenu for my ListBoxItems. So i created this: <ListBox Name=listBoxName> <ListBox.ItemTemplate>

  • 0

i wanted a contextmenu for my ListBoxItems.
So i created this:

<ListBox Name="listBoxName">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding UserName}" />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                    <ListBox.ItemContainerStyle>
                        <Style TargetType="ListBoxItem">
                            <Setter Property="ContextMenu">
                                <Setter.Value>
                                    <ContextMenu>
                                        <MenuItem Header="View" Name="MenuItemView" />
                                    </ContextMenu>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </ListBox.ItemContainerStyle>
                </ListBox>

This works great. I have the contextmenu for all items, but if i want to add a click-eventhandler to the menuitem, like this:

<MenuItem Header="View" Name="MenuItemView" Click="MenuItemView_Click" />

I get a XamlParseException when the window is created.

InnerException:
The Object System.Windows.Controls.MenuItem cannot be converted to type System.Windows.Controls.Grid

It throws only the exception if i add a event-handler. The event-method is empty.

Edit:
Stacktrace of the InnerException:

at
Chat_Client.ChatWindow.System.Windows.Markup.IComponentConnector.Connect(Int32
connectionId, Object target) in
c:\XXX\Chat_Client\ChatWindow.xaml:Row
19.

at
MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetConnectionId(Object
root, Int32 connectionId, Object
instance)

Edit2:
Now i have to get the object I clicked with the contextmenu. First i tried this:

//MenuItem s = sender as MenuItem;
//ContextMenu cm = s.Parent as ContextMenu;
//Popup pu = cm.Parent as Popup;
//object o = pu.Parent;

But the Popup’s parent is null.
Then i simply get the selectedItem from the ListBox. This works, but is there no way to get the ListBoxRow of the clicked Contextmenu?

  • 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-18T22:49:42+00:00Added an answer on May 18, 2026 at 10:49 pm

    I cannot reproduce your crash with VS2010 and WPF4.

    You only need one context menu for all your items so you can extract it to a window resource, for example:

    <Window.Resources>
        <ContextMenu x:Key="ListBoxItemContextMenu">
            <MenuItem Header="View" Name="MenuItemView" Click="MenuItemView_Click"/>
        </ContextMenu>
    </Window.Resources>
    

    and then change your setter to refer to that one context menu:

    <Setter Property="ContextMenu" Value="{StaticResource ListBoxItemContextMenu}"/>
    

    and the event handler then works:

    private void MenuItemView_Click(object sender, RoutedEventArgs e)
    {
        Debug.WriteLine("Clicked!");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to show the users Name Address (see www.ipchicken.com ), but the only
I wanted to use a Rich Text Editor for a text area inside an
Wanted to share a way I found to remove strong name from assembly name.
I wanted to bring this challenge to the attention of the stackoverflow community. The
Wanted to create a fun app with photo. This is the scenario: I have
I wanted to add a contextmenu to a tabitem. But it should only be
Wanted to know, because side effect of this could be to alert user that
I wanted to know if this is possible? I would like to create a
I have a best practices question. I realize this is subjective but wanted to
I wanted to create a search add-on to right click and search tag text

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.