DialogResult can be set only after Window is created and shown as dialog

susu picture susu · May 4, 2011 · Viewed 7.4k times · Source

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

Answer

susu picture susu · May 5, 2011

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