Saturday, 14 September 2013

MouseEnter ContextMenu.StaysOpen doesn't respond

MouseEnter ContextMenu.StaysOpen doesn't respond

I've build a WPF application that contains a button, which activates a
drop-down sub-menu. My intent for the button behavior was to make it so
when it is moused-over, the sub-menu appears and you can access it.
Likewise, when the mouse goes off it, the sub-menu disappears. Pretty
basic function.

I'm tried using the MouseEnter xaml and this is where my problem occurs. I
tell the contextmenu to stay open using ContextMenu.IsOpen = true , then
call ContextMenu.StaysOpen = false in the MouseLeave to close it.
private void AdminButton_MouseEnter(object sender, MouseEventArgs e)
{
(sender as Button).ContextMenu.IsEnabled = true;
(sender as Button).ContextMenu.PlacementTarget = (sender as Button);
(sender as Button).ContextMenu.Placement =
System.Windows.Controls.Primitives.PlacementMode.Bottom;
(sender as Button).ContextMenu.IsOpen = true;
(sender as Button).ContextMenu.StaysOpen = false;
}
private void AdminButton_MouseLeave(object sender, MouseEventArgs e)
{
(sender as Button).ContextMenu.StaysOpen = false;
}
The result is that once I mouse over the button, it stays active and the
submenu exists until I click elsewhere on the screen. I'm pretty confused
by this behavior, shouldn't the ContextMenu.StaysOpen = false close the
submenu?
I've already tried to use both MouseEnter/MouseLeave and the most common
behavior is that the second the cursor comes off the button to access the
submenu, the submenu disappears.
I figure if I can't make these commands work, there must be some way to
suspend the MouseLeave duration so the user can get to the submenu before
it closes???
not many examples on the web concerning this action for a button or
textblock, so any help would be great.

No comments:

Post a Comment