WPF Popup not causing application to be focused when clicked on

popup, wpf

Solution

If you handle `MouseLeftButtonDown` event, you can call `Window.Activate()` method. but you should write it for each element - `Popup` and for all `TextBlock`s.

The problem you can met with is that on Windows you can swap mouse buttons, where left became right and vice versa (but I don't know how this works), so, may be you have to handle `MouseRightButtonDown` event.

Problem

I have a control that is using a popup with some WPF controls inside of it, and StaysOpen="True". The problem is when clicking on the popup when the application doesn't have focus, the application does not receive focus. I've done a bit of research, and it seems like this may be due to the fact that popups are meant to be used for menus, so they don't have all of the proper windows message handlers hooked up. Here is a barebones sample to demoing the problem: ``` <Window x:Class="TestWindowPopupBehavior.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:TestWindowPopupBehavior="clr-namespace:TestWindowPopupBehavior" Title="MainWindow" Height="350" Width="525"> <Grid> <Popup StaysOpen="True" IsOpen="True" Placement="Center"> <ListBox> <TextBlock>123</TextBlock> <TextBlock>123</TextBlock> <TextBlock>123</TextBlock> <TextBlock>123</TextBlock> <TextBlock>123</TextBlock> <TextBlock>123</TextBlock> </ListBox> </Popup> </Grid> </Window> ``` - Run the application. - Interact with the listbox, which should work fine. - Switch to another application. - Click on the listbox while the application is not active. Nothing happens - Click the application outside of the listbox. - Click on the listbox. It is working now. What I would expect to happen in step 4 would be the application would receive focus, and the listbox would select the new item. Are there any workarounds to this problem, or something obvious I'm missing? I'm looking at rewriting the whole popup code with full-fledged windows, and re-implementing the behavior we have, but that seems really complicated just to fix a small problem like this.

Original source