How to make a WPF window be on top of all other wi

2019-01-23 00:37发布

I want my window to be on top of all other windows in my application only. If I set the TopMost property of a window, it becomes on top of all windows of all applications and I don't want that.

标签: c# wpf topmost
17条回答
你好瞎i
2楼-- · 2019-01-23 00:55

I too faced the same problem and followed Google to this question. Recently I found the following worked for me.

CustomWindow cw = new CustomWindow();
cw.Owner = this;
cw.ShowDialog();
查看更多
萌系小妹纸
3楼-- · 2019-01-23 00:56

Instead you can use a Popup that will be TopMost always, decorate it similar to a Window and to attach it completely with your Application handle the LocationChanged event of your main Window and set IsOpen property of Popup to false.

Edit:

I hope you want something like this:

    Window1 window;

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        window = new Window1();
        window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
        window.Topmost = true;
        this.LocationChanged+=OnLocationchanged;
        window.Show();
    }

    private void OnLocationchanged(object sender, EventArgs e)
    {
          if(window!=null)
              window.Close();
    }

Hope it helps!!!

查看更多
\"骚年 ilove
4楼-- · 2019-01-23 00:56

There are several threads, there's even a "topmost" tag. Search on that, or go directly to this post which looks good:

How to keep a window on top of all other windows in my application only?

查看更多
▲ chillily
5楼-- · 2019-01-23 00:58

Here's a way to do it: make your "topmost" window subscribe to your other windows GotFocus and LostFocus events and use the following as the event handlers:

class TopMostWindow
{
    void OtherWindow_LostFocus(object sender, EventArgs e)
    {
        this.Topmost = false;
    }

    void OtherWindow_GotFocus(object sender, EventArgs e)
    {
        this.Topmost = true;
    }
}
查看更多
干净又极端
6楼-- · 2019-01-23 01:01

You can add this to your windows tags

WindowStartupLocation="CenterScreen"

Then you can also display it if you want your users to acknowledge it in order to proceed

YourWindow.ShowDialog();

First try it without TopMost parameters and see the results.

查看更多
Juvenile、少年°
7楼-- · 2019-01-23 01:03

Try this:

Popup.PlacementTarget = sender as UIElement;
查看更多
登录 后发表回答