Hi devz,
While using a Microsoft.Win32 Dialog (OpenDialog or SaveDialog), it is possible that it will be opened behind the Main Window. So a good way to go back on that dialog is to use the ALT+TAB combo. But not really intuitive for the end user.
The best way would be that this dialog is always on top of the Main Window.
This is the way 😎
var sd = new SaveFileDialog
{
FileName = "test.txt",
Filter = "Txt file|*.txt",
Title = "Save file"
};
bool? dialogResult = sd.ShowDialog(Window.GetWindow(Application.Current.MainWindow));
if (dialogResult != null && dialogResult == true)
fileName = sd.FileName;
Code language: C# (cs)
The key here is to tell your dialog which window is its parent. To do that you can simply pass the parent window as a parameter to your dialog.
To get the Main Window (even from a ViewModel), you can use this:
Window.GetWindow(Application.Current.MainWindow)
Not pure MVVM, I known, but I don’t feel like a heavy cheater doing that. 😉
Happy coding!