WPF is really getting me out of my nerves here. I configured a popup with some complex content (grid, buttons etc..). I s开发者_Go百科et 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.
精彩评论