C#WPF - 没有集中注意力的Popup

时间:2018-03-12 13:41:04

标签: c# wpf popup focus

我只是试图制作一个弹出窗口,在角落里显示一条消息。这个Popup是我用" TopMost"所取得的每一个窗口的顶部,但我似乎无法让那些不可聚焦的东西工作......

我的PopUp 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>

我如何称呼它(来自另一个窗口):

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

PopUp代码:

public Popup(string text)
{
      InitializeComponent();

      label.Content = text;
}

1 个答案:

答案 0 :(得分:2)

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

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

或者如果你是从另一个窗口调用它:

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

但是我必须说:这不是MVVM,因为你在Window类中存储文本,我强烈建议你开始阅读MVVM。