Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Popup not causing application to be focused when clicked on

Tags:

wpf

popup

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>
  1. Run the application.
  2. Interact with the listbox, which should work fine.
  3. Switch to another application.
  4. Click on the listbox while the application is not active. Nothing happens
  5. Click the application outside of the listbox.
  6. 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.

like image 625
FryGuy Avatar asked Apr 24 '12 01:04

FryGuy


1 Answers

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

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.

like image 127
stukselbax Avatar answered Oct 02 '22 03:10

stukselbax