WPF Popup not causing application to be focused when clicked on

Today I came across this issue myself.

If you want, you can fix it globally by registering a class handler on the Popup class itself in the app.xaml.cs:

C#:

/// <inheritdoc />
protected override void OnStartup(StartupEventArgs e)
{
    EventManager.RegisterClassHandler(typeof(Popup), UIElement.PreviewMouseDownEvent, new RoutedEventHandler(Popup_PreviewMouseDownEvent));
}

/// <summary>
/// Ensures that the application is activated before the <see cref="UIElement.MouseDownEvent"/> is invoked on the Popup.
/// This solves an issue where the Popup seemed to be frozen if you focused out to another application and clicked back in the Popup itself.
/// </summary>
/// <param name="_"></param>
/// <param name="__"></param>
private void Popup_PreviewMouseDownEvent(object _, RoutedEventArgs __)
{
    Current?.MainWindow?.Activate();
}

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.

Tags:

Wpf

Popup