簡體   English   中英

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

[英]C# WPF - Popup which doesn't get focused

我只是試圖制作一個彈出窗口,在角落里顯示一條消息。 這個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;
}

您必須將Mainwindow定義為Popup的所有者並將StartupLocation屬性居中。 像這樣的東西:

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

或者,如果您從另一個窗口調用它:

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

但是我必須說:這不是MVVM,因為你在Window類中存儲文本,我強烈建議你開始閱讀有關MVVM的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM