How to make a WPF window on top of all other windows of my application (and not throughout the system)?

I want my window to be on top of all other windows only in my application . If I set the TopMost property of a window, it gets on top of all windows of all applications, and I do not want this.

+56
c # wpf topmost
Mar 30 '10 at 15:43
source share
18 answers

You need to set the window owner property.

You can show the window through showdialog to lock your main window, or you can show it in normal mode and have it on top of the owner without blocking the owner.

here is the codeexample part of the code. I left all the obvious things:

namespace StackoverflowExample { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } void NewWindowAsDialog(object sender, RoutedEventArgs e) { Window myOwnedDialog = new Window(); myOwnedDialog.Owner = this; myOwnedDialog.ShowDialog(); } void NormalNewWindow(object sender, RoutedEventArgs e) { Window myOwnedWindow = new Window(); myOwnedWindow.Owner = this; myOwnedWindow.Show(); } } } 
+40
Jun 18 '12 at 18:40
source share
โ€” -

Instead, you can use a popup that will always be TopMost, decorate it like a window and fully attach it with your application, handle the LocationChanged event of your main window and set the IsOpen Popup property 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 this helps!

+15
Mar 30 '10 at 16:16
source share
 CustomWindow cw = new CustomWindow(); cw.Owner = Application.Current.MainWindow; cw.ShowInTaskbar = false; cw.ShowDialog() ; 
+11
Mar 27 '12 at 7:18
source share

Just do it in XAML , and was surprised that no one had sent this answer yet. In the following example, Window is defined in a ResourceLibrary (note x:Key ), but you can also use this XAML binding on a stand-alone WPF Page resource.

 <Window x:Key="other_window" Topmost="{Binding Source={x:Static Application.Current},Path=MainWindow.IsActive,Mode=OneWay}"> <TextBlock Text="OTHER WINDOW" /> </Window> 
+6
Jul 04 '16 at 21:21
source share

use the Activate () method. This is trying to bring the window to the fore and activate it. e.g. Window wnd = new xyz (); wnd.Activate ();

+5
Dec 6 '10 at 16:40
source share

The best way to set these two events for all windows of your application:

 GotKeyboardFocus LostKeyboardFocus 

in the following way:

 WiondowOfMyApp_GotKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e) { windowThatShouldBeTopMost.TopMost = true; } WiondowOfMyApp_LostKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e) { windowThatShouldBeTopMost.TopMost = false; } 
  • and, of course, all the windows that you would like to be at the top should be accessible from other windows of your application. in my case, I have a base window and a few more windows that should be at the top of my base window, so it was nice that my base window had an instance of every other window.
+5
31 Oct. '11 at 6:43
source share

I faced the same situation as you. Most search queries I came across that all I had to do was set the Owner of the windows that I want to be the topmost in the main window or something else called Show.

In any case, I will go ahead and post a solution that worked for me.

I created event handlers for Window.Activated and Window.Deactived in a window that should have been Topmost in relation to my application.

 private void Window_Activated(object sender, EventArgs e) { Topmost = true; } private void Window_Deactived(object sender, EventArgs e) { if(Owner == null || Owner.IsActive) return; bool hasActiveWindow = false; foreach(Window ownedWindow in Owner.OwnedWindows) { if(ownedWindow.IsActive) hasActiveWindow = true; } if(!hasActiveWindow) Topmost = false; } 

This works great for me. Hope this is helpful to someone else .: O)

+4
Aug 01 '11 at 16:01
source share

In the pop-up window, it overloads the Show () method with the parameter:

 Public Overloads Sub Show(Caller As Window) Me.Owner = Caller MyBase.Show() End Sub 

Then in the main window, call your overloaded Show () method:

 Dim Popup As PopupWindow Popup = New PopupWindow Popup.Show(Me) 
+4
Oct 28 '11 at 19:31
source share

There are several threads, there is even a "topmost" tag. Find this or go straight to this post that looks good:

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

+2
Mar 30 '10 at 15:52
source share

I am OP. After some research and testing, the answer is:

No, there is no way to do just that.

+1
Apr 08 '10 at 16:15
source share

Here's how to do it: make your โ€œtopmostโ€ window subscribe to your other GotFocus and LostFocus windows and use the following events as handlers:

 class TopMostWindow { void OtherWindow_LostFocus(object sender, EventArgs e) { this.Topmost = false; } void OtherWindow_GotFocus(object sender, EventArgs e) { this.Topmost = true; } } 
+1
Jul 04 '10 at 19:09
source share

You can add this to your Windows tags.

 WindowStartupLocation="CenterScreen" 

Then you can also display it if you want your users to confirm it in order to continue.

 YourWindow.ShowDialog(); 

First try it without TopMost options and see the results.

0
Mar 30 '10 at 20:00
source share

Try the following:

 Popup.PlacementTarget = sender as UIElement; 
0
Oct 30 '13 at 5:43 on
source share

I also ran into the same issue and followed Google on this. I recently found this to work for me.

 CustomWindow cw = new CustomWindow(); cw.Owner = this; cw.ShowDialog(); 
0
Jun 27 '15 at 7:48
source share

How about htis:

 Private Sub ArrangeWindows(Order As Window()) For I As Integer = 1 To Order.Length -1 Order(I).Owner = Order(I - 1) Next End Sub 
0
15 Oct '16 at 11:38
source share

I ran into this problem. I have a desktop application that has several WPF windows, and I needed my own splash screen, which would be on top of other windows only in my application. No other windows open when my splash screen appears, but I open MainWindow from my splash screen after some authentication. So I just did something similar to what @GlenSlayden did, but in code, because, as I said, MainWindow is not attached to me for:

 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, when my program loads all the other windows from MainWindow, the splash screen is on top, but while the program authenticates the user, it is not the top one, so you can click on any other program and it will hide behind it. This is the closest I could find to solve this problem. It is not perfect, because it still runs on top of all other applications while my program loads after authentication, but this is not very long in my case.

0
Nov 20 '17 at 17:25
source share

Just studied C # and ran into a similar situation. but found a solution that I think can help. Perhaps you understood this a long time ago. it will be from the start of a new project, but you can use it in any.

1) Start a new project.

2) go to Project, then New Windows form, then select Windows Form and name it Splash.

3) set the size, background, text, etc. Optional.

4) In the properties of the Splash.cs form, set the starting position: CenterScreen and TopMost: true

5) add form1 "using System.Threading;"

6) Form1 in the class add "Splash splashscreen = new Splash ();"

7) form1 add "splashscreen.Show ();" and "Application.DoEvents ();"

8) form1 In the section Events >> Focus >> Activated add "Thread.Sleep (4000); splashscreen.Close ();"

9) add Splash.cs to "Public Splash" add "this.BackColor = Color.Aqua;" / any color can be used

10) This is the code for Form1.cs

 public partial class Form1 : Form { Splash splashscreen = new Splash(); public Form1() { InitializeComponent(); splashscreen.Show(); Application.DoEvents(); } private void Form1_Activated(object sender, EventArgs e) { Thread.Sleep(4000); splashscreen.Close(); } } 

11) this is the code for Splash.cs

 public partial class Splash : Form { public Splash() { InitializeComponent(); this.BackColor = Color.Aqua; } } 

12) I found that if you do not do anything in the splash screen, the screen will not remain top for the time that you need to activate the first form. The thread counter will disappear after x seconds, so your program is working fine.

0
May 20 '18 at 22:42
source share

I just ran into the same problem and found that I could not configure the owner using MVVM without causing the application to crash. I have a window manager view model that has a command to open a window using a window URI - and I could not set the owner of App.MainWindow without crashing the application.

Therefore - Instead of setting the owner, I associated the TopMost property of the window with a property in my Window Manager view model that indicates whether the application is currently active. If the application is active, the window is on top, as I would like. If it is not active, other windows may close it.

Here is what I added to my view model:

  public class WindowManagerVM : GalaSoft.MvvmLight.ViewModelBase { public WindowManagerVM() { App.Current.Activated += (s, e) => IsAppActive = true; App.Current.Deactivated += (s, e) => IsAppActive = false; } private bool _isAppActive = true; public bool IsAppActive { get => _isAppActive; set { if (_isAppActive != value) { _isAppActive = value; RaisePropertyChanged(() => IsAppActive); } } } } 

Here is the XAML that implements it (I use MVVM light with ViewModelLocator as a static resource in my application called Locator):

 <Window Topmost="{Binding WindowManager.IsAppActive, Source={StaticResource Locator}}"/> 
0
Feb 11 '19 at 16:21
source share



All Articles