如何使WPF弹出式窗口不被隐藏在主应用程序? [英] How to make WPF pop-up windows not be hidden behind main application?

查看:1052
本文介绍了如何使WPF弹出式窗口不被隐藏在主应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个WPF应用程序,我有一个弹出窗口的实例按钮。

In a WPF application, I have buttons which pop up instances of windows.


  • 我点击第一个按钮,然后在第一个窗口中的前的主应用程序的正确弹出。

  • 我点击第二个按钮和第二个窗口弹出的正确的。然而,在第一窗口现在移动背后主应用程序。这是混乱和意外,因为它往往是在主应用程序的中间,这样的似乎消失,直到用户移动主应用程序找到它背后隐藏的。

  • I click the first button and the first window pops up correctly in front of the main application.
  • I click the second button and the second window pops up correct in front of the main application. However, the first window now moves behind the main application. This is confusing and unexpected since it is often in the middle of the main application and thus seems that it disappears until the user moves the main application to find it hiding behind.

这是在 XAML

<Window x:Class="TestPopupFix.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="600" Width="800">
    <StackPanel>
        <Button Content="Open first popup" Click="Button_OpenFirst"/>
        <Button Content="Open second popup" Click="Button_OpenSecond"/>
    </StackPanel>
</Window>

和背后这在代码:

And this the code behind:

private void Button_OpenFirst(object sender, RoutedEventArgs e)
{
    Window window = new Window();
    TextBlock tb = new TextBlock();
    tb.Text = "This is the first window.";
    window.Content = tb;
    window.Width = 300;
    window.Height = 300;
    window.Show();
}

private void Button_OpenSecond(object sender, RoutedEventArgs e)
{
    Window window = new Window();
    TextBlock tb = new TextBlock();
    tb.Text = "This is the second window.";
    window.Content = tb;
    window.Width = 300;
    window.Height = 300;
    window.Show();
}



什么我必须做,使主要应用住宿最远到后面,我弹出新窗口?

推荐答案

要在视觉层次安排窗口,你必须设置子窗口的父窗口的所有者属性。

To arrange windows in a visual hierarchy you have to set the Owner property of the child window to the parent window.

您可以阅读更多有关的 http://msdn.microsoft.com/library/system.windows.window.owner.aspx\">the 所有者属性。

You can read more about the Owner property on MSDN.

您应该改变你的代码转换成类似于这样:

You should change your code into something similar to this:

Window parentWindow;

private void Button_OpenFirst(object sender, RoutedEventArgs e)
{
  this.parentWindow = new Window();
  this.parentWindow.Owner = this;
  this.parentWindow.Show();
}

private void Button_OpenSecond(object sender, RoutedEventArgs e)
{
  Window childWindow = new Window();
  childWindow.Owner = this.parentWindow;
  childWindow.Show();
}

这篇关于如何使WPF弹出式窗口不被隐藏在主应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆