Reputation: 3000
As the title says, Is there anyway to set DialogResult without closing a Dialog?
Probably the easy answer is NO, but even though, Why is that? What is the behaviour behind DialogResult=
? Is there any way to override it.
For anyone who asks, why I even need something like this, the simple answer is curiosity. The 'history' behind this curiosity is that I am trying to call dialog windows from my ViewModel.My approach is a mixture of the following
In cases that the dialog has a return value (True (Ok) / False (Cancel)), I need to pass (from dialog viewModel) that value the class that handles the Dialogs and then set it as the DialogResult
in order for the parent window / control to get it when the dialog returns / closes.
The a minimal code snippet to test it, is something like
dialog.DialogResult = TheResult;
Debug.WriteLine("Dialog Closed");
dialog.Close();
While this does not fail, the dialog closes, when the "Dialog Closed" is printed to the Output, before calling dialog.Close();
. If I override the Window.Closing
event, I can see it firing when calling dialog.DialogResult
but not when the dialog.Close();
gets called afterwards.
Note: As I said the whole question is 'out of curiosity' since the code above works and does not harm the desirable functionality.
Upvotes: 4
Views: 4018
Reputation: 993
This is the expected behavior. Have a look at the following
https://marlongrech.wordpress.com/2008/05/28/wpf-dialogs-and-dialogresult/
Specifically ...
"Once you set this property on the Window the Window will automatically close and the ShowDialog methods returns the result that you have set in the DialogResult property.
Because DialogResult is returning the result of the dialog it makes sense that setting the value of DialogResult would signal the end of the dialog's use.
Setting DialogResult triggered a Close. The dialog will be closed once the current method has completed. This means Debug.WriteLine is executed as well as dialog.Close().
You would see the same if you ran the following in a button click
this.Close();
MessageBox.Show("TEST");
this.Close();
The first call to Close will trigger the close and fire the Closing and Closed events, the code continues execution so the message box is displayed, the second call to Close is called but it ignored because a close was already called. It won't fire any more events.
This is the code I tested with, where I opened MainWindow using ShowDialog in the OnStartup event in App.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
//Or call Close
//this.Close();
MessageBox.Show("TEST");
this.Close();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBox.Show("Window_Closing");
}
private void Window_Closed(object sender, EventArgs e)
{
MessageBox.Show("Window_Closed");
}
}
Upvotes: 1
Reputation: 8786
This is by design, set the DialogResult will close the Dialog. Check the Source Code for Window.DialogResult
public Nullable<bool> DialogResult { get { .... } set { .... if (_dialogResult != value) { _dialogResult = value; if(_isClosing == false) { Close(); } } ... } } }
Upvotes: 5