Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect that a Window has used up its "ShowDialog" call

Tags:

c#

wpf

In WPF you get to call ShowDialog on a window exactly once. After that it is done for.

Seems kind of lame to me, but those are the rules. If you call ShowDialog again you get this exception:

Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed

What I want to know is: How can I take a Window (or UserControl really) and check to see if it has had ShowDialog called (so I know to new up a different one before calling ShowDialog again).

Something like this:

public void ShowListOfClients()
{
    //    |  This is the method I want to write
    //    V                                          
    RefreshViewIfNeeded(_myWindowOrUserControlThatShowsAList);

    FillWindowWithBusinessData(_myWindowOrUserControlThatShowsAList);
    _myWindowOrUserControlThatShowsAList.ShowDialog();

}

NOTE: Clearly in the above example it would be easier to just create a new WindowOrUserControlThatShowsAList every time I enter the method. But please consider the question more that the dumbed down example.

like image 475
Vaccano Avatar asked Nov 14 '10 21:11

Vaccano


1 Answers

This isn't exclusive to ShowDialog(), Show() does it too. And no, there is no IsDisposed property to check. IsLoaded is only half a solution, it will be false for the 1st invocation as well.

First approach is to just make a dialog that can be re-shown:

    public bool CloseAllowed { get; set; }

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
        if (!CloseAllowed) {
            this.Visibility = System.Windows.Visibility.Hidden;
            e.Cancel = true;
        }
    }

The next one is to explicitly keep track of the health of the object reference:

    private Window1 win = new Window1();   // say

    private void button1_Click(object sender, RoutedEventArgs e) {
        if (win == null) {
            win = new Window1();
            win.Closing += delegate { win = null; };
        }
        win.ShowDialog();
    }
like image 174
Hans Passant Avatar answered Sep 20 '22 19:09

Hans Passant