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

  • Home
  • SEARCH
  • 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 6996549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:10:17+00:00 2026-05-27T20:10:17+00:00

I have hooked up a context menu item in a listbox item such that

  • 0

I have hooked up a context menu item in a listbox item such that tapping it will change its state. I need either the menu to stay open after the item has been selected, or to programmatically reopen the menu right after it closes.

My menu looks like so:

Some Command 1
Some Command 2
Some Command 3
Inverted

And the user can tap the Inverted command and then tap one of the other commands to cause them to function in Inverted Mode, and the menu through data binding appears like so:

Some Command 1
Some Command 2
Some Command 3
Inverted ✔ 

Not being able to figure out how to keep the menu open after tap, I’ve tried the less ideal reopen menu approach like so:

private void onCommandInvert(object sender, RoutedEventArgs e)
{
  CommandState.Instance.Inverted = !CommandState.Instance.Inverted;

  // Open it again.
  MenuItem menuItem = (MenuItem)sender;
  ContextMenu menu = (ContextMenu)menuItem.Parent;
  menu.IsOpen = true;
}

But doing so throws the following exception on the menu.IsOpen = true statement:

A first chance exception of type 'System.InvalidOperationException' occurred in 
  System.Windows.dll

An unhandled exception of type 'System.InvalidOperationException' occurred in 
  System.Windows.dll

Additional information: Element is already the child of another element.

I have also tried the following with the Closed event, with the same exception occurring:

private void onContextMenuClosed(object sender, RoutedEventArgs e)
{
  ContextMenu menu = (ContextMenu)sender;
  menu.IsOpen = true;
}

Any ideas? Thanks!

  • 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-27T20:10:18+00:00Added an answer on May 27, 2026 at 8:10 pm

    I got it! Thanks to willmel’s comment, I digged through the source code for the MenuItem and was able to override OnClick() to do exactly what I needed (the ideal solution no doubt). I couldn’t access Click however, so I needed to introduce a StayClick event property as well.

    Enjoy!

    using Microsoft.Phone.Controls;
    using System.Windows;
    
    namespace MyNamespace
    {
      public class MenuItemEx : MenuItem
      {
        public bool StayOpenWhenClicked
        {
          get;
          set;
        }
    
        public event RoutedEventHandler StayClick;
    
        protected override void OnClick()
        {
          if (StayOpenWhenClicked)
          {
            if (StayClick != null)
            {
              StayClick.Invoke(this, new RoutedEventArgs());
            }
          }
          else
          {
            base.OnClick();
          }
        }
      }
    }
    

    and in the page’s xaml, instead of toolkit:MenuItem you use my:MenuItemEx

    <my:MenuItemEx 
      Header="Inverted"             
      StayClick="onCommandInvert"
      StayOpenWhenClicked="True"
    />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a NSWindow that hosts a WebView that Ive hooked up to a
I have a form that is closed unexpectedly immediately after showing it. I hooked
I have a function hooked on a $('form').submit() event. This function will fire an
In my Sitecore 6.1.0 installation I have hooked onto the item:added event by implementing
I have a GridView control hooked up to a datasource that I allow row
I have an asp textbox that I have hooked up to a jQuery calendar
I'm using the PureMVC framework on a FlexBuilder 3 project. I have already hooked
How can I set a parameter of a sub-report? I have successfully hooked myself
I have 2 keyboards hooked up to my machine (one on PS/2, the other
If I have a printer hooked directly to a pc (a kiosk with a

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.