5 Replies - 7806 Views - Last Post: 16 July 2013 - 12:59 PM Rate Topic: -----

#1 Eric_afterDark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-July 13

Unable to close custom ContextMenu window

Posted 16 July 2013 - 10:37 AM

Everything in my WPF program is generally custom. In this problem I have a custom menu button that acts kind of like the MS Office button from 2007. When clicked it brings down a context menu. My problem is trying to get it to close. After being opened, the Context Menu never closes. I would like if it would close when the user clicks anywhere outside of the menu box, like how a normal Context Menu would act.

This is what I have so far:

.xaml Window:

<!--- MAIN MENU BUTTON -->
        <Button Click="Button_Click_1" Margin="5,4,0,0" Name="Button_1" Height="50" VerticalAlignment="Top" HorizontalAlignment="Left" Width="49.758">
            <!--- MAIN MENU -->
            <Button.ContextMenu>
                <ContextMenu x:Name="MainContextMenu" PlacementRectangle="{Binding RelativeSource={RelativeSource Self}}" MouseLeave="ContextMenuMouseLeave" >
                    ...
            </Button.ContextMenu>


Right now I am just trying to get it to close when the mouse leaves the menu, because I do not know what keyword to use when I am describing a click outside of the Context menu. As you can see, when the mouse leaves the menu, the program calls the "ContextMenuMouseLeave" function.

.xaml.cs code-behind:

//CLOSE CONTEXT MENU
        private void ContextMenuMouseLeave(object sender, EventArgs e)
        {
            ContextMenu.Close();
        }


Apparently in WPF the Context Menu object does not contain
Close()
, or
Hide()
. I have also tried
MyContextMenu.IsOpen = false;
and
SendKeys.SendWait("{ESC}");
with no luck. How do I get this menu window to close again in WPF??

Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Unable to close custom ContextMenu window

#2 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6408
  • View blog
  • Posts: 25,889
  • Joined: 12-December 12

Re: Unable to close custom ContextMenu window

Posted 16 July 2013 - 11:10 AM

I am confused. The ContextMenu is what appears when you right-click something/somewhere. aka the shortcut-menu. It should not appear in a main menu (if it does, then it shouldn't be a ContextMenu IMO).

If you have correctly attached a ContextMenu to an element then it will automatically appear and disappear as the element is right-clicked, and clicked away from; it shouldn't require you to close it.

This post has been edited by andrewsw: 16 July 2013 - 11:12 AM

Was This Post Helpful? 0
  • +
  • -

#3 Eric_afterDark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-July 13

Re: Unable to close custom ContextMenu window

Posted 16 July 2013 - 11:16 AM

It is a Context Menu that appears on left and right click, and only disappears, now that you mention it, on a right click outside of the menu ONLY after being opened with a right click.

If I should not use a Context Menu as my main menu, what kind of drop down menu should I attach to my button? Changing the menu type could solve my problem.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6408
  • View blog
  • Posts: 25,889
  • Joined: 12-December 12

Re: Unable to close custom ContextMenu window

Posted 16 July 2013 - 11:21 AM

I'm not using WPF myself, but I assume it could be some kind of Panel. You might wait for a colleague to respond.

This post has been edited by andrewsw: 16 July 2013 - 11:26 AM

Was This Post Helpful? 0
  • +
  • -

#5 Eric_afterDark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-July 13

Re: Unable to close custom ContextMenu window

Posted 16 July 2013 - 11:23 AM

Well, I'm keeping what I have until I can find a better solution. Thank you.
Was This Post Helpful? 0
  • +
  • -

#6 Eric_afterDark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-July 13

Re: Unable to close custom ContextMenu window

Posted 16 July 2013 - 12:59 PM

Sorry for the double post (couldn't find an EDIT button)

I solved my problem by setting
MainContextMenu.Visibility = Visibility.Collapsed;
on mouseLeave and then set
MainContextMenu.Visibility = Visibility.Visible;
on buttonclick.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1