Home > Enterprise >  Why MainWindow doesn't take the focus from a Popup child
Why MainWindow doesn't take the focus from a Popup child

Time:03-10

I have a problem when I'm using a Textbox in a Popup, opened from the MainWindow: if the MainWindow doesn't have the focus after the Popup has been opened, I'm not able to write something in the TextBox even if I click on it before. It seems that the click action doesn't give the focus to the MainWindow because it opacity is still LightGray. You can find below a sample VB.Net code to be able to reproduce this issue:

MainWindow.xaml

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" Height="400" Width="800" Title="FooWindow">

    <Grid>
        <Button Click="Button_Click" Content="Settings" Margin="10" />
    </Grid>
</Window>

MainWindow.xaml.vb

Imports System.Windows.Controls.Primitives

Class MainWindow
Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
End Sub

Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    Dim popup = New Popup()
    popup.HorizontalAlignment = HorizontalAlignment.Center
    popup.VerticalAlignment = VerticalAlignment.Center
    popup.PlacementTarget = Me
    popup.Width = 250
    popup.Height = 250
    popup.StaysOpen = True
    popup.Child = New TextBox() With {.Padding = New Thickness(20), .Height = 80, .Width = 100, .VerticalAlignment = VerticalAlignment.Center, .HorizontalAlignment = HorizontalAlignment.Center,
        .VerticalContentAlignment = VerticalAlignment.Center}
    popup.IsOpen = True
End Sub
End Class

How to reproduce this issue?

  • Click on the button
  • Click on the TextBox
  • Write something
  • Click on another Window (on your computer)
  • Click on the TextBox again
  • -> You'll not be able to write something again

CodePudding user response:

You can work around this by adding an handler for the Popup's PreviewMouseLeftButtonDown event that activates the window:

Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    Dim popup = New Popup()
    popup.HorizontalAlignment = HorizontalAlignment.Center
    popup.VerticalAlignment = VerticalAlignment.Center
    popup.PlacementTarget = Me
    popup.Width = 250
    popup.Height = 250
    popup.StaysOpen = True
    popup.Child = New TextBox() With {.Padding = New Thickness(20), .Height = 80, .Width = 100, .VerticalAlignment = VerticalAlignment.Center, .HorizontalAlignment = HorizontalAlignment.Center,
            .VerticalContentAlignment = VerticalAlignment.Center}
    AddHandler popup.PreviewMouseLeftButtonDown, AddressOf OnPopupMouseLeftButtonDown
    popup.IsOpen = True
End Sub

Private Sub OnPopupMouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs)
    Activate()
End Sub
  • Related