Search code examples
c#wpfprogressdialogdialogresult

DialogResult handling


I have a question about accessing dialogdata from wpf/ I have a ProgressDialog :System.Windows.Window And I am calling it in OnButtonClick like this:

        void OnButtonClick(object sender, RoutedEventArgs e)
        {   
            ProgressDialog dlg = new ProgressDialog("");
            //dlg.AutoIncrementInterval = 0;
            LibWrap lwrap = new LibWrap();
            DoWorkEventHandler handler = delegate { BitmapFrame bf = lwrap.engine(frame); };
            dlg.CurrentLibWrap = lwrap;
            dlg.AutoIncrementInterval = 100;
            dlg.IsCancellingEnabled = true;
            dlg.Owner = Application.Current.MainWindow;
            dlg.RunWorkerThread(0, handler);
}

The question is - how to check in this function(OnButtonClick) if the DialogResult is OK (in other words - how to access dlg's inner fields after it finish execution)?


Solution

  • DialogResult usually is no inner field but a rather public property, so dlg.DialogResult should be fine (given that it inherits from Window), you will need to cast it to a bool.

    I do not see you open the window anywhere, if you use ShowDialog the return value is automatically the DialogResult and the calling thread blocks until it gets closed.

    var result = (bool)dlg.ShowDialog();
    

    If you need a non-modal dialog you can use Show subscribe to the Closed event and check the DialogResult there.

    dlg.Closed += (_,__) =>
    {
        var result = (bool)dlg.ShowDialog();
        // Do something with it.
    }
    dlg.Show();
    

    Of course the dialog needs to set the property in either case. Default actions like Alt+f4 set it to false.