How to make a WPF window be on top of all other windows of my app (not system wide)?
Solution 1:
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();
}
}
}
Solution 2:
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!!!
Solution 3:
CustomWindow cw = new CustomWindow();
cw.Owner = Application.Current.MainWindow;
cw.ShowInTaskbar = false;
cw.ShowDialog() ;