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
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.
Just learning C# and ran across similar situation. but found a solution that I think may help. You may have figured this a long time ago. this will be from starting a new project but you can use it in any.
1) Start new project.
2) go to Project, then New Windows form, then select Windows Form and name Splash.
3) set size, background, text, etc as desired.
4) Under Properties of the Splash.cs form set Start Position: CenterScreen and TopMost: true
5) form1 add "using System.Threading;"
6) form1 under class add "Splash splashscreen = new Splash();"
7) form1 add "splashscreen.Show();" and "Application.DoEvents();"
8) form1 Under Events>>Focus>>Activated add "Thread.Sleep(4000); splashscreen.Close();"
9) Splash.cs add under "Public Splash" add "this.BackColor = Color.Aqua;" /can use any color
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 on Splash.cs
public partial class Splash : Form
{
public Splash()
{
InitializeComponent();
this.BackColor = Color.Aqua;
}
}
12) I found that if you do NOT do something in the splash then the screen will not stay on the top for the time the first form needs to activate. The Thread count will disappear the splash after x seconds, so your program is normal.
Simple to do it in XAML, and surprised that nobody posted this answer yet. In the following example, the Window
is defined in a ResourceLibrary
(notice the x:Key
), but you can also use this XAML binding on a standalone Page
-style WPF resource.
<Window x:Key="other_window"
Topmost="{Binding Source={x:Static Application.Current},Path=MainWindow.IsActive,Mode=OneWay}">
<TextBlock Text="OTHER WINDOW" />
</Window>
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?
CustomWindow cw = new CustomWindow();
cw.Owner = Application.Current.MainWindow;
cw.ShowInTaskbar = false;
cw.ShowDialog() ;
I ran into a very similar situation as you. Most of the searches I came across stated all I needed to do was set the Owner of the windows I wish to be Topmost to the main window or whatever window that called Show.
Anyways, I'll go ahead and post a solution that worked well for me.
I created event handlers for Window.Activated and Window.Deactived in the window that was supposed to be Topmost with respect 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;
}
It works great for me. Hopefully this is useful to someone else out there. :o)