C#WPF-不显示的弹出窗口';我不能集中注意力
我只是想做一个小弹出窗口,在角落里显示一条消息。这个弹出窗口应该在我用“TopMost”实现的其他窗口的顶部,但我似乎无法让不可聚焦的东西工作 我的弹出式XAML:C#WPF-不显示的弹出窗口';我不能集中注意力,c#,wpf,popup,focus,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"
<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;
}
弹出代码:
<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的内容。请阅读代码。如果没有看到您迄今为止编写的代码或XAML,没有人能就您遗漏的内容或尝试的其他内容提供好的建议。如果没有一些张贴的代码或对您尝试的无效内容的非常详细的解释,这个问题很可能很快就会结束。好的,我编辑了这个问题。这是正确的答案。请按此标记;)