How to make a WPF window be on top of all other windows of my app (not system wide)?

后端 未结 19 3026
南笙
南笙 2020-12-14 05:42

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 al

19条回答
  •  醉酒成梦
    2020-12-14 06:01

    I just ran into this same issue. I have a desktop app that has multiple WPF windows, and I needed my custom splash screen to be on top of the other windows in my app only. No other windows are open when my splash screen comes up, but I do open the MainWindow from my splash screen after some authentication. So I just did something similar to what @GlenSlayden did but in code behind since, like I said, the MainWindow isn't up for me to bind to:

    private void SplashScreen_ContentRendered(object sender, EventArgs e)
    {
        // User authentication...
        // ...
    
        MainWindow mainWindow = new MainWindow();
        SetBinding(SplashScreen.TopmostProperty, new Binding("IsVisible"))
        {
            Source = mainWindow,
            Mode = BindingMode.OneWay,
            UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
        };
        mainWindow.Show();
    }
    

    Now while my program is loading all the other windows from the MainWindow, the splash screen is on top, but while the program is authenticating the user, it's not topmost, so you can click away on some other program and it will hide behind it. It's the closest thing I could find to a solution for this problem. It's not perfect because it still goes over top of all other applications while my program is loading after authentication, but that's not for very long in my case.

提交回复
热议问题