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

标签 c# wpf popup focus

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

我的弹出窗口 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();
}

弹出代码:

public Popup(string text)
{
      InitializeComponent();

      label.Content = text;
}

最佳答案

您必须将主窗口定义为弹出窗口的所有者,并将 StartupLocation 属性居中。像这样:

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

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

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

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

关于C# WPF - 没有获得焦点的弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49236696/

相关文章:

c# - 从托管项目加载时,如何单步执行非托管 DLL 中的方法? (视觉 C++/C#)

c# - 使用 IComparable 对对象进行排序

c# - 为什么这个 C# 计时器代码不起作用?

c# - 与 MVVM 的用户界面交互

c# - 在按钮单击上下文菜单中如何绑定(bind)到 viewModel?

python - Matplotlib basemap : Popup box

javascript - 使弹出模式对话框在出现后可移动/可拖动

c# - 从单个域管理员帐户使用 O365 REST API 访问另一个用户日历

C# 异步逻辑到同步调用

Javascript 在子窗口的就绪回调中给出了错误的值