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
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;
}
Try this:
Popup.PlacementTarget = sender as UIElement;
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.
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();
}
}
}
I'm the OP. After some research and testing, the answer is:
No, there is no way to do exactly that.
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)