Clicking on Popup outside bounds of TaskPane loses keyboard focus - wpf

I'm developing a Microsoft Word TaskPane containing a WPF ElementHost. The hosted WPF uses a Popup that is intended to behave as a ComboBox's does. I believe you can reproduce my problem like this:
popup.IsOpen = true;
popup.StaysOpen = false;
Mouse.Capture(ancestor_of_popup, CaptureMode.SubTree);
When a region of the Popup lies outside the bounds of the TaskPane, click the mouse in that region. The entire TaskPane loses keyboard focus to Word's main window, but retains mouse capture. At this point, you can use the Popup with a mouse while typing on the keyboard edits your document!
If the TaskPane loses keyboard focus then the Popup should close, but then any control in the region of a Popup that lies outside the bounds of the TaskPane becomes unusable. Any ideas?

This looks like a similar problem to Popups in XBAP stop receiving anything but mouse events after losing focus?.
Apparently there is a known bug with focus in WPF popup controls, although that may be specific to XBAP (you didn't specify if this was a WPF desktop or browser application).

Related

WPF Popup not automatically closing when raised from a ListBoxItem

I have a User Control with an image in it and when the user clicks the image I use a popup to display a record card. I set StaysOpen="False" and all works perfectly through my application, except when I host it in a ListBox. In this case the Popup refuses to recognise a click away from it. Is there an issue with raising a popup from a ListBoxItem or are there other ways around this?
Maybe, opening Popup using Interactivity commands instead of using a Button could be the issue.
Interactivity is not doing the mouse capture and keyboard focus actions. So, Popup will not have a proper mouse capture reference by using Interactivity commands.
I did find the answer in the end and t was my mistake. The action to raise the popup was done on Mouse Down and the popup was opening and then closing on the mouse up cation. All I did was to change it to Mouse Up and it was fine. Thank you for your answer anyway.

Winforms TextBox gets focus but doesn't capture keyboard input

This has me baffled.
I've written a popup box which consists of a WinForms UserControl hosted inside a WindowsFormsHost, which, in turn, is hosted in a Primitives.Popup which is displayed on the screen. The whole application is WPF, but this control was lifted from an earlier application written in WinForms.
The popup is activated by an external event (an incoming phone call from a CTI Server).
Inside the UserControl is a textbox control. When the user clicks in the text box, I call the Focus method on the Popup, then call the Focus method on the textbox. The textbox gets the focus. I can be fairly sure of that because the box shows a cursor after clicking in it, and also I have a "GotFocus" event handler that prints a debugging message.
However, if there was another program active at the time the incoming event occurs, any keys that are pressed on the keyboard continue to go to that program, not to the text box. Only if the user clicks in another part of my application (i.e., part of the screen outside the popup) to make it the active program, and then clicks in the text box is the text box able to receive keyboard input.
I hope I've given enough information without overwhelming you with the myriad of details. If there's something else anyone needs to point me in the right direction, I'll be happy to provide it.
Because the WinForm TextBox is hosted, setting focus on it does not activate the hosting WPF Window. Add a line to activate the Window.
private void TextBox_Click(object sender, EventArgs e)
{
this.Activate(); //activate the Window
(sender as System.Windows.Forms.TextBox).Focus();
}

MouseLeave of underlying object is fired even when overlapping object is IsHitTestVisible = false

I have created a custom popup to decorate my buttons with animated tooltips. I track Button.MouseEnter for the button to decide when to display the popup. I use Button.MouseLeave to determine when to hide the popup.
Problem is Button.MouseLeave is fired prematurely if the popup moves over the mouse cursor (its appearance is animated) despite the fact that I have set IsHitTestVisible = false for the popup and all its visual children.
Is this the way WPF is designed to work? I need MouseLeave to only fire when the cursor moves away from the button itself and not be influenced by the popup.
Thanks
I believe that the Popup control is actually contained within a window, which is why the popup can extend beyond the window bounds in some cases. (It's also why popup transparency is not supported in Silverlight.)
I believe that while the popup control is no longer processing "hits", the container window is, which is why you are losing your button's mouse focus.
I've not tested this, but you might try creating a template for your button and actually declaring the popup as part of the button (rather than below it). This may cause WPF to view the popup control as part of the button and eliminate the problem of losing mouse focus. This works in other scenarios, but I'm not 100% sure how this will work with a Popup.
EDIT: As a side note, the deault WPF tooltip allows you to override the template. I'm not sure what your goals are, but you may find it easier to change the appearance and behavior of the default tooltip than to try to roll your own, as a lot of these sorts of problems have already been solved in the default Tooltip.

WPF Popup and WindowsFormsHost Problem

I am hosting windowsforms control in WPF popup. Problems below:
If i make StaysOpen=False i can't interact with winform control. StaysOpen to false is required because when clicked outsidet the Popup region, it should close.
if i make StaysOpen=True i can interact with winform control but when i click outside the area of popup, it is not getting closed.
I tried setting StaysOpen=true in MouseEnter of popup and StaysOpen=False in MouseLeave, but MouseLeave fires as and when mouse is over winform control resulting in unexpected behaviour.
I even tried IsMouseCaptureWithin property of popup and found it does not work with winforms (i guess its a bug in framework).
Another problem, i was trying to close popup when root main form (which is windows form) is deactivated (pressed Alt+Tab), but this event (deactivate) is fired even when i enter into one of the controls in windowshostControl in popup.
Desired Behaviour:
should be able to host and interact with winform control in wpf popup.
on clicking on outside the area of popup, popup should close.
Appreciate any inputs.
Thanks.
I've had many problems with the defacto-standard popups in WPF, because they are in fact a new window with their own handle. This means if you drag your application around the screen, the popup stays put (it doesn't move with your window). It also means your popup has some strange behaviors and doesn't interact with your application in ways other controls normally do.
I've created 2 decorator classes to address this problem:
PopupDecorator.cs and
TimeoutPopupDecorator.cs
It's pretty simple to use:
Add a namespace declaration for the new popup classes. i.e.
xmlns:dday_wpf="clr-namespace:DDay.WPF"
Surround the area you want the popup to be able to be displayed with the decorator. i.e.
<dday_wpf:PopupDecorator x:Name="popup">
<dday_wpf:PopupDecorator.Popup>
... contents of popup go here ...
</dday_wpf:PopupDecorator.Popup>
... contents of panel go here ...
</dday_wpf:PopupDecorator>
It works pretty much identically to a normal Popup from that moment on.
This may not solve all your problems, but hopefully it helps.
This sounds a bit like my problem launching a modeless winform control from a WPF form.
Check out my question Why is my WPF textbox "kinda" readonly?.
The just being, based on what Doug said about popups being a window with its own handle, makes this applicable.

WPF Mouse behavior when ComboBox is focused

When the WPF ComboBox is clicked and in-focus, the only interaction that occurs after that can be with the ComboBox. If anything else is interacted with, including the window functions (minimize, restore, close, resize) and any control in the window, the action is ignored and the ComboBox loses focus.
In addition, MouseEnter and MouseLeave on the window buttons are still active, but when MouseEnter on the window border(?) occurs, the mouse pointer does not change to the resize pointer. This behavior makes sense because of the ComboBox's use of the popup control. The popup control exists independently of the main visual tree and if i.e. the window moves or gets resized, the popup remains fixed floating above the main window.
I have tried using Reflector, to see what the ComboBox is doing, but I have not been able to find what I am looking for. Basically, I do not know if this behavior is coming from the window, the ComboBox, or if it has something to do with the popup. How can I solve this problem?
You're right on in your description there, the popup keeps all action focus until it itself loses focus. If you're trying to change the functionality of the ComboBox you may want to look at creating your own ControlTemplate that behaves differently and does not keep the default action of the popup control.
Hope this helps, not entirely sure what you're trying to do.
I know this question is old, but for anyone coming here looking for the answer, it is to use Mouse.Capture.
The ComboBox sets Mouse.Capture(comboBox, CaptureMode.SubTree) in OnIsDropDownOpenChanged. This ensures that all mouse events are captured by the ComboBox. When the Popup is closed Mouse.Capture(null) releases the mouse capture.

Resources