Popup Never Closes - wpf

WPF is really getting me out of my nerves here. I configured a popup with some complex content (grid, buttons etc..). I set its StaysOpen property to False and IsOpen to True on a textbox MouseDown preview event.
Ths thing is that it opens but never closes when clicking anywhere outside the window.
Any suggestions?
Thanks!
UPDATE:
My popup has buttons inside. When I click one of those, the popup closes when I click outside of it. Is some weird stuff happening to the events routing?

Looks like the popup won't close if opened by any other control event. I just binded the IsOpen property to the IsChecked property of a ToggleButton to simulate a combobox.
Thanks for all your answers.

I was also having this issue, except on the PreviewMouseButtonUp event of a Button. The assumption that there is some bug with Popups and trying to open them in Tunneling events was accurate and led me down the path to my fix (which is a little more generic).
I needed to resolve this (the host control was generic / could be several types of controls) by listening for the bubbling event instead of the tunneling event, specifically with the AddHandler(RoutedEvent,Delegate,Boolean) method in order to capture handled events.
WAG the issue lies somewhere when transitioning from tunneling to bubbling.

I use code behind to initialize popup, and I've found that it's not closed if ran sync from other UI action like mouse event. To workaround this I run it async:
public static void ShowPopupMessage(string message)
{
DispatcherHelper.UIDispatcher.BeginInvoke(new Action(() =>
{
var popup = new Popup
{
Child = new AutoHideMessage(message),
StaysOpen = false,
IsOpen = true
};
}));
}

I set IsOpen on a textbox MouseDown preview event.
Set it to what? And where is the TextBox hosted?
I can only guess with the scant information provided, but I'd say when you click outside the Popup, your event handler is firing and opening it up again.

You can use LostFocus event of the PopUp. If the focus is not within the popup, set its IsOpen to false to close it.

Related

Strange issue - mouse click in popup is getting captured by control underneath

I'm displaying a Popup in response to a button click (popup.IsOpen = true;). The popup contains a ComboBox, and when I click an item in the combobox, one of the things the SelectionChanged event does is to hide the popup.
The Popup appears over a DataGrid that I also have on my page, and I'm finding that the mouse-click on the combobox is also being picked up by a MouseUp event that I've got on the DataGrid. Any idea what's going on?
The MouseUp Event has a routing strategy of type Bubbling. Events that use this type of strategy get passed up the chain to parent controls. Since the Popup is a child of the DataGrid, the event will "bubble" up to the DataGrid. If you would rather the event not bubble, you can try using PreviewMouseUp, which has a Tunneling routing strategy, and will "tunnel" down the chain to child controls. Here is a decent overview of Routing Strategies.
I've hit the same issue. Oddly, it doesn't happen when the code is run in the debugger - it only happens in the release version. It really seems to be a bug in WPF. Trying to catch the click and set the event to handled doesn't work.
My workaround is to, when the popup opens, to tell the control underneath to ignore the click.

WP7 - Cancelling ContextMenu click event propagation

I'm having a problem when the Silverlight toolkit's ContextMenu is clicked while it is over a UIElement that has registered a Tap event GestureListener. The context menu click propagates to the underlying element and fires its tap event.
For instance, say I have a ListBox and each ListBoxItem within it has registered both a ContextMenu and a Tap GestureListener. Assume that clicking context menu item2 is supposed to take you to Page1.xaml, while tapping on any of ListBox items themselves is supposed to take you to Page2.xaml.
If I open the context menu on item1 in the ListBox, then context menu item2 is on top of ListBox item2. When I click on context menu item2 I get weird behavior where the app navigates to Page1.xaml and then immediately to Page2.xaml because the click event also triggered the Tap gesture for ListBox item2.
I've verified in the debugger that it is always the context menu that receives the click event first. How do I cancel the context menu item click's routed event propagation so it doesn't reach ListBox item2?
Thanks for your help!
You can get around the problem by doing the following:
In the context menu's Opened handler set LayoutRoot.IsHitTestVisible (LayoutRoot is the default name for the root UIElement) to false
In the context menu's Closed handler set LayoutRoot.IsHitTestVisible back to true
You could try adding a rectangle with a transparent background (important) over the effected area/page when showing the context menu.
I had a very similar issue, but I am using the ManipulationCompleted event as a "tap" detector as the object the ContentMenu applies to is a custom control.
LayoutRoot.IsHitTestVisible didn't work for me, perhaps because it does not apply to Manipulation events. However, it set me on the right path. I just implemented my own simple equivalent of it - I created a boolean variable bCancelManipulation in the page's scope.
In the ContentMenu's Opened event set it to True.
In the ContentMenu's Closed event set it to False.
In the ManipulationCompleted function, the first thing I do is check
if(bCancelManipulation==true) { return; }
It's kind of a hack, but it works great and is quite simple to code - it can easily be adapted to a Tap event too.

RibbonComboBox MouseLeave Event doesnt fire

I cant get mouseleave to fire inside a ribboncombobox inside a ribboncontrolgroup inside ribbon tab inside a ribbon.
I have a behavior that on mouse enter opens the dropdown and should close it on mouseleave
except mouseleave doesnt fire I verified this using snoop and by setting a breakpoint on the event.
Any ideas or workarounds?
I was having a similiar issue with the border control - and I found an event called, "IsMouseDirectlyOverChanged" - that seemed to fire pretty regularily so I used that.

Popup window and context menu

I am using the ToolStripDropDown to host the user control as the pop-up window. The problem is when a context menu strip is displayed from within this pop-up window, the pop-up itself closes in the moment the context menu opens.
I have tried to subclass the ContextMenuStrip and added WS_EX_NOACTIVATE to CreateParams but nothing changed. First I thought that there is no way to do this since it is common behavior but then I tried to put a TextBox class onto the pop-up user control and invoke the Edit control context menu - and the parent pop-up window did not close.
What am I missing?
Had a similary Problem. On my UserControll was a toolstrip. When I pressed the toolsstripdropdownbutton the dropdown was shown but the popup disapeared.
The reason was that popup.Autoclose was true. After Setting to false the Popup is not closed any more.
ToolStripDropDown popup = new ToolStripDropDown();
popup.AutoClose = false; //Set to FALSE
popup.Margin = Padding.Empty;
popup.Padding = Padding.Empty;
ToolStripControlHost host = new ToolStripControlHost(userControl1);
host.Margin = Padding.Empty;
host.Padding = Padding.Empty;
popup.Items.Add(host);
popup.Show(button1, new Point(100,100));
Actual Solution should be the one in Martin's final comment:
Use ContextMenu Instead of ContextMenuStrip
That one worked for me, and the ToolStripDropDown no longer closes by itself when right clicking one of its content controls, like it should. We still need it to AutoClose, disabling AutoClose on ToolStripDropDown will do bad things, it is supposed to close on losing focus. Example: open any other app window, and the ToolStripDropDown will continue to appear on top

How do I disable the exit button on a Silverlight 3 Child Window?

I mean the small exit/cancel button marked with an X in the top right hand corner. I want to implement a Logon dialog box that accepts a username/password so obviously I don't want the user to be able to dismiss the modal pop up. If it is not possible to remove or disable the button then is there some way I can intercept the closing event and stop it closing?
You can use the HasCloseButton property of the ChildWindow to hide the close button.
Please let me know if this helps.
Ezequiel Jadib
The code below prevents a ChildWindow from ever closing, effectively disabling the X button. Modify to suit your business logic.
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
base.OnClosing(e);
e.Cancel = true;
}
Select child window and Press F4. It will show the property window. Then goto HasCloseButton property and uncheck the checkbox.
Enjoy
HasCloseButton="False" ..
This property used to hide the 'X' button In ChildWindow

Resources