RadMenu does not close menu upon click

6 posts, 0 answers
  1. Ed
    Ed avatar
    10 posts
    Member since:
    Jan 2012

    Posted 31 May 2012 Link to this post

    Hi,

    I have a RadMenu like this:

    <telerik:RadMenu>
        <telerik:RadMenuItem Header="File">
            <telerik:RadMenuItem Header="New" Command="{Binding Path=NewCommand}" />
        </telerik:RadMenuItem>
    </telerik:RadMenu>

    And a command in the viewmodel like this:

    NewCommand = new DelegateCommand(delegate()
        {
            MessageBox.Show("This is a test", "Message", MessageBoxButton.OK);
        });

    When I click on the New menu item, it opens the message box but it leaves the menu open (see attached screenshot). The menu only closes when I click OK in the message box.

    The same is true if I change the MessageBox.Show line to Thread.Sleep(3000) - the menu remains open until 3 seconds have elapsed.

    Instead I want the menu to close before the command is invoked. How can I do this?

    Many thanks

    Ed
  2. Lancelot
    Lancelot avatar
    251 posts
    Member since:
    Jul 2012

    Posted 04 Jun 2012 Link to this post

    Hi Ed,

    I have seen this issue before and I know the Telerik team is working on a solution.  I have found and example for you that might close the menu at the time you want it to.

    Use this method in the event handler where you want the menu to close, it is a workaround that causes the menu to collapse.

    RadMenuItemAutomationPeer CloseME = new RadMenuItemAutomationPeer(menuItemRoot);
     CloseME.Invoke();


    Let em know if this didnt work for you and we'll dig in deeper.

    Good Luck!
    Lancelot
  3. DevCraft banner
  4. Ed
    Ed avatar
    10 posts
    Member since:
    Jan 2012

    Posted 11 Jun 2012 Link to this post

    Hi Lancelot,

    Thanks for looking into this for me. However, it turns out that this code essentially re-clicks the menu item rather than closing the menu.

    Can Telerik confirm that this is a known issue or provide a workaround?

    Many thanks

    Ed
  5. Lancelot
    Lancelot avatar
    251 posts
    Member since:
    Jul 2012

    Posted 11 Jun 2012 Link to this post

    Hi Ed,

    When you call for MessageBox.Show, it blocks the calling thread, so the UI won't update.

    Use the dispatcher to change the thread you are using for the messagebox. This should solve the problem of the the menu not closing.

    Call the MessageBox like this:

    Dispatcher.BeginInvoke(() => MessageBox.Show(“This is a test”));


    Good Luck,
    Lancelot
  6. Ed
    Ed avatar
    10 posts
    Member since:
    Jan 2012

    Posted 14 Jun 2012 Link to this post

    Hi Lancelot,

    Thanks for the big clue! It turned out that just wrapping the call with Dispatcher.Invoke still had the same problem. I had to spawn a new thread and start it with a Thread.Sleep to allow the menu to close, as follows:

    public MyViewModel()
    {
      NewCommand = new DelegateCommand(delegate()
        {
          Thread thread = new Thread(new ThreadStart(delegate()
            {
              Thread.Sleep(30);
              Deployment.Current.Dispatcher.BeginInvoke(() => MessageBox.Show("This is a test"));
            }));
          thread.IsBackground = true;
          thread.Start();
        });
    }

    This seems to work - hope there aren't any unintended consequences when I invoke a longer method with more points of user interaction.

    It would still be good to hear from Telerik as to whether there is another way. The 30ms sleep time was chosen arbitrarily.

    Many thanks

    Ed
  7. George
    Admin
    George avatar
    1332 posts

    Posted 19 Jun 2012 Link to this post

    Hi Ed,

     
    You could use only the Dispatcher without running the code in a new thread - it should do the trick. Please, refer to the attachment where you can find a sample project with a video. 

    Hope this helps.

    All the best,
    George
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top
DevCraft banner