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
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}}"/>
use the Activate() method. This attempts to bring the window to the foreground and activate it. e.g. Window wnd = new xyz(); wnd.Activate();
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();
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;
}
}
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
This is what helped me:
Window selector = new Window ();
selector.Show();
selector.Activate();
selector.Topmost = true;