3

I've encountered a weird behavior in WPF. Even though there are quite a few ways to avoid this problem, I'm trying to better understand why it's happening:

I created a new WPF application, just added a button which has a ContextMenu:

<Grid>
    <Button x:Name="btnTest" Margin="10,10,10,10" 
            MouseEnter="BtnTest_OnMouseEnter" MouseLeave="BtnTest_OnMouseLeave">
        <Button.ContextMenu>
            <ContextMenu x:Name="myContext">
                <TextBlock Text="Context Menu Text"></TextBlock>
            </ContextMenu>
        </Button.ContextMenu>
    </Button>      
</Grid>

In the code behind I use MouseEnter to show the ContextMenu and MouseLeave to hide it:

private void BtnTest_OnMouseEnter(object sender, MouseEventArgs e)
{
    myContext.PlacementTarget = btnTest;
    myContext.Placement = PlacementMode.Bottom;
    myContext.IsOpen = true;
}

private void BtnTest_OnMouseLeave(object sender, MouseEventArgs e)
{
    myContext.IsOpen = false;
}

So now - I see the ContextMenu under the button when the mouse is on the button and it hides when the mouse leaves the button.

BUT when I click the button I get an exception

An unhandled exception of type 'System.StackOverflowException' occurred in WindowsBase.dll

Question is - Why is the Mouse Click, specifically, triggering this exception? I don't have any code of mine running on the Click event, yet without clicking an exception doesn't occur...

BTW: Same will happen if I replace the Button with an Image for instance, so it doesn't seem to be caused by a specific control...

6
  • Is there an advantage of using a ContextMenu over a ToolTip in this scenario? Commented Dec 16, 2012 at 15:20
  • I'd disagree with your interpretation that your code 'works fine'. It does not. While the mouse is over the button, the Enter and Leave event handlers are being called continuously. Opening the context menu causes the mouse to 'leave' the button, which causes the context menu to close, which causes the mouse to 'enter' the button, which causes the context menu to open.... Is this question purely out of curiosity or do you have a genuine problem to solve here? Commented Dec 16, 2012 at 15:56
  • @LukeWoodward It's more out of curiosity as to why does the click itself cause a stackoverflow. Also, I edited the "works fine" comment Commented Dec 16, 2012 at 16:04
  • I can't really say why there's an exception if you click but not if you hover. It's likely that the only people who could answer this are the people who actually wrote WPF. I am not one of those people. Commented Dec 16, 2012 at 16:18
  • 1
    Have you tried to look at the stack trace when the exception occurs, to find a repeating pattern? Commented Dec 16, 2012 at 22:20

1 Answer 1

-1

Change your XAML like this:

   <Grid>
        <Popup x:Name="myContext">
            <TextBlock Text="Context Menu Text"></TextBlock>
        </Popup>

    <Button x:Name="btnTest" Margin="10,10,10,10" 
        MouseEnter="BtnTest_OnMouseEnter" MouseLeave="BtnTest_OnMouseLeave">
    </Button>      
    </Grid>

I think there is a loop of this sort going on in your code:

  1. you enter the button, the popup shows
  2. you click, popup hides (default behavior of contextmenu)
  3. button gets focus, popup is shown again

What happens if you set the ´StaysOpen´ property of the ContextMenu? If you then dont get this behavior anymore my suspicion is correct.

Sign up to request clarification or add additional context in comments.

6 Comments

Note you need a <Grid.ContextMenu> to wrap it in also... But I'm afraid it doesn't help. Same problem when it's inside the grid... Also I'm real curious as to WHY this is happening
This should remain a ContextMenu, even though using a Popup might solve this problem, it's not the answer I'm looking for... (and dosn't answer WHY this is happening) Thanks though :)
Popup and contextmenu are the same AFAIK (apart from the right click behavior), so there is no benefit in your use case using a contextmenu because you dont need that right click behavior
That's not true, they don't even derive from the same primitive class..!
whats the functional difference then?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.