gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:41:21 24 4
gpt4 key购买 nike

我只是想制作一个小弹出窗口,在角落显示一条消息。这个弹出窗口应该在我用“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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com