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.
You need to set the owner property of the window.
You can show a window via showdialog in order to block your main window, or you can show it normal and have it ontop of the owner without blocking the owner.
here is a codeexample of the codebehind part - I left out all obvious stuff:
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();
}
}
}
myOwnedDialog.ShowInTaskbar = false;
in to NewWindowAsDialog
–
Towne 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!!!
CustomWindow cw = new CustomWindow();
cw.Owner = Application.Current.MainWindow;
cw.ShowInTaskbar = false;
cw.ShowDialog() ;
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>
use the Activate() method. This attempts to bring the window to the foreground and activate it. e.g. Window wnd = new xyz(); wnd.Activate();
In the popup window, overloads the method Show() with a parameter:
Public Overloads Sub Show(Caller As Window)
Me.Owner = Caller
MyBase.Show()
End Sub
Then in the Main window, call your overloaded method Show():
Dim Popup As PopupWindow
Popup = New PopupWindow
Popup.Show(Me)
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)
The best way is set this two events to all of windows of your app:
GotKeyboardFocus
LostKeyboardFocus
in this 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 surely all of the windows that you wanted to be top, should be accessible from other windows of your app. in my case I have a base window and another some windows that should be top of my base window, so this was not bad to my base window has instance of each another windows.
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?
I'm the OP. After some research and testing, the answer is:
No, there is no way to do exactly that.
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;
}
}
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.
Try this:
Popup.PlacementTarget = sender as UIElement;
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();
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
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.
I just ran into the same problem and found trouble setting the owner using MVVM without causing the app to crash in production. I have a Window Manager View Model that includes a command to open a window using the uri of the window - and I wasn't able to set the owner to App.MainWindow without the app crashing.
So - Instead of setting the owner, I bound the TopMost property of the window to a property in my Window Manager View model which 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 can cover 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 a ViewModelLocator as a static resource in my app called Locator):
<Window Topmost="{Binding WindowManager.IsAppActive, Source={StaticResource Locator}}"/>
This is what helped me:
Window selector = new Window ();
selector.Show();
selector.Activate();
selector.Topmost = true;
This is my second answer to the question; I have found another very effective technique for managing multiple Window
instances belonging to a single Application
As many have noted, the Window.Owner
property is key. Whenever your app is active--meaning one of its windows IsActive
--you want the Owner
property of all your windows to be null
. This allows equal switching between the various windows in your app.
But when your app is not active, you want the Window.Owner
property for each of your non-MainWindow
windows to be set to your MainWindow
. This way, you can use any of your app's windows to re-activate, and all your app's windows will be restored in front of all other apps.
Each time the app is re-activated, switch the Window.Owner
values back to null
. the Application.OnActivated
and Application.OnDeactivated
methods are perfect for this.
You'll have to separately keep a list of all your windows, since using this workaround means you won't have the convenience of the Window.OwnedWindows
property on your MainWindow
, for when it comes time to perform these adjustments.
public partial class MyApp : Application
{
// not shown: keep this list of all your non-main windows updated...
public List<Window> rgw = new List<Window>();
protected override void OnActivated(EventArgs e)
{
// when app is ACTIVE, set all `Owner` values to null...
rgw.ForEach(w => w.Owner = null);
base.OnActivated(e);
}
protected override void OnDeactivated(EventArgs e)
{
// set `Owner` on all non-main Windows only when not-active
rgw.ForEach(w => w.Owner = Current.MainWindow);
base.OnDeactivated(e);
}
// etc..
}
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.
© 2022 - 2024 — McMap. All rights reserved.