C# WPF - 没有获得焦点的弹出窗口

C# WPF - Popup which doesn't get focused

我只是想制作一个小弹出窗口,在角落显示一条消息。这个 Popup 应该在我用 "TopMost" 实现的所有其他 Window 的顶部,但我似乎无法让无法聚焦的东西工作......

我的弹出窗口XAML:

<Window x:Class="message.Popup"
    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"
    xmlns:local="clr-namespace:message"
    mc:Ignorable="d"
    Title="Popup" Height="129.808" Width="300" Focusable="False" Topmost="True">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="179*"/>
        <ColumnDefinition Width="113*"/>
    </Grid.ColumnDefinitions>
    <Label x:Name="label" Content="" HorizontalAlignment="Left" Margin="10,40,0,0" VerticalAlignment="Top" Width="272" Grid.ColumnSpan="2"/>

</Grid>

我怎么称呼它(来自另一个Window):

private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
      new Popup(textBox.Text).Show();
}

弹出代码:

public Popup(string text)
{
      InitializeComponent();

      label.Content = text;
}

您必须将 Mainwindow 定义为 Popup 的所有者,并将 StartupLocation 属性 居中。像这样:

            PopUpWindow.Owner = MainWindow;
            PopUpWindow.WindowStartupLocation = WindowStartupLocation.CenterOwner;

或者,如果您是从另一个 window 调用它:

            PopUpWindow.Owner = this;
            PopUpWindow.WindowStartupLocation = WindowStartupLocation.CenterOwner;

但是我必须说:这不是 MVVM,因为您将文本存储在 Window class 中,我强烈建议您开始阅读有关 MVVM 的内容。