4

I have a main WPF window, mywindow.showDialog when a button is clicked on the window, a command is executed let's say the command is SendToTableCommand

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;
    }
}

Sometime ago, m_OwningWindow.DialogResult = true throws exception. So I added an if check that uses IsThreadModal. It has worked for a while, but now m_OwningWindowdoes not close because IsThreadModal is false.

I do not know what's the right way to solve the issue and think I did not handle it properly. Please help. thanks in advance

CodeNaked
  • 40,753
  • 6
  • 122
  • 148
susu
  • 73
  • 1
  • 5

3 Answers3

1

Jason's reply reminds me of a workaround. i.e. using Window.Close(), then add a bool type property on window, say OKClicked, replace anywhere that set DialogResult with window.Close(); window.OKClicked = true or false. replace reference to window.DialogResult with window.OKClicked. Any problem with the workaround? thanks

susu
  • 73
  • 1
  • 5
  • 1
    my main concern is if it's safe to reference property of a window after window.close()? there is no Disposed property on System.Windows.Window – susu May 05 '11 at 16:37
0

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

Edit: And the window should be closed, not hidden. That bit me after I posted.

Grault
  • 974
  • 12
  • 31
-1

Use Form.Modal to determine if your form is being opened as a window or a modal dialog.

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

Also, DialogResult is an enumerated type - true isn't a value I'd expect to see being assigned to it. Typically DialogResult.OK or DialogResult.Yes would be used for this.

Jason Williams
  • 56,972
  • 11
  • 108
  • 137
  • 5
    thanks. It is not form. It is WPF window and there is no Modal property, I can use window.Close to close it but I want to do different things based on if users click OK button or Cancel to exit the window. DialogResult on WPF window is bool? not enumerate – susu May 05 '11 at 15:30