DialogResult can only be installed after creating a window and displaying it as a dialog

I have the main WPF window, mywindow.showDialog when the button in the window is pressed, the command is executed, say, the SendToTableCommand command

protected virtual void SendToTableExecute(object o) { UIThread.BeginInvoke(new Action<object>(SendToTableExecuteUI),o); } private void SendToTableExecuteUI(object o) { if (o is Control) { m_OwningWindow = UIHelper.FindVisualParent<Window>((Control)o); } do sth... if (m_OwningWindow != null) { //only set DialogResult when window is ShowDialog before if(System.Windows.Interop.ComponentDispatcher.IsThreadModal) m_OwningWindow.DialogResult = true; } } 

Once, m_OwningWindow.DialogResult = true throws an exception. Therefore, I added an if check using IsThreadModal. It worked for a while, but now m_OwningWindow is not closing because IsThreadModal is false.

I do not know how to solve the problem correctly and I think that I did not deal with it properly. Please help. thank you in advance

+4
source share
3 answers

Jason's answer recalls a workaround. that is, using Window.Close (), then add a property of type bool to the window, say OKClicked, replace wherever DialogResult is installed with the window. Close (); window.OKClicked = true or false. replace the link for window.DialogResult with window.OKClick. Any problem with a workaround? thanks

+1
source

I hid my window before assigning DialogResult . The order is rearranged, so DialogResult is assigned before the window is hidden, my problem is fixed. Even if the window was ShowDialog 'd, it must be considered "open" to set DialogResult .

Change The window should be closed, not hidden. This is a bit of me after I posted.

0
source

Use Form.Modal to determine if your form opens as a window or modal dialog.

You should be able to close () the form when you want to close it, regardless of whether it is a dialog or not. (Under certain circumstances, you may also need the Utility after closing)

In addition, DialogResult is an enumerated type - true not the value that I expect to see assigned to it. Typically, DialogResult.OK or DialogResult.Yes are used for this.

-1
source

All Articles