How can I make sure only one WPF Window is open at a time?

15,457

Solution 1

Instead of searching the static application objects, you could instead just track this within your window, with a single static variable. Just keep a variable in the window:

private static frmCaseWpf openWindow = null; // Assuming your class name is frmCaseWpf

When you create a window, either in the initialize routines, or OnLoaded, depending on how you want it to work..:

partial class frmCaseWpf {
    public frmCaseWpf {
         this.OnLoaded += frmCaseWpf_OnLoaded;
    }

    private void frmCaseWpf_OnLoaded(object sender, RoutedEventArgs e)
    {
         if (this.openWindow != null)
         {
              // Show message box, active this.openWindow, close this
         }
         this.openWindow = this;
    }
}

If you want this window to be reusable, make sure to set this.openWindow = null; when you close the window, as well.

Solution 2

Here's something that's working for me.

    private About aboutWin;
    private void AboutOpenClicked(object sender, RoutedEventArgs e)
    {
       if(aboutWin == null)
       {
           aboutWin = new About();
           aboutWin.Closed += (a, b) => aboutWin = null;
           aboutWin.Show();
       }
       else
       {

           aboutWin.Show();  
       }

    }
Share:
15,457
Russ
Author by

Russ

As a key member of any technology architecture team I do what it takes to accomplish technical and business goals with quality, efficiency, and accountability. I specialize in providing technology leadership that inspires while bringing new ideas, and technologies that result in high performance, high availability enterprise products. My primary goal has always been to improve efficiency and productivity for the company through direct contributions as well as the technical development and mentorship of the team working with me. I bridge the Communication gap with leadership, team leads, project managers, and internal partners to guarantee reliable and regular software delivery. While simultaneously bringing a broad level of expertise in both agile software development and system integration, as well as a big picture approach to development with organizational goals and measurable business results.

Updated on June 05, 2022

Comments

  • Russ
    Russ almost 2 years

    I have a WPF window that I am launching from inside of a winform app. I only want to allow once instance of that WPF window to be open at a time, and not warn that user if they try to open it again.

    I am having a problem however trying to search for that WPF window being open because the window is being launched from a winform. What I normaly do is when searching for a winform, I search for any instances of that winform existing in the Application.Current.OpenForms, and when in WPF I search for Application.Current.Windows

    The problem I have is that System.Windows.Application.Current is null when launched from inside of a winform, so I can't search for the WPF window that way. Is there any better way of searching for an existing instance of an open window?

    My Code:

    if (System.Windows.Application.Current != null)
                    {
                        foreach (System.Windows.Window win in System.Windows.Application.Current.Windows)
                        {
                            if (win is frmCaseWpf)
                            {
                                MessageBox.Show("You may have only one active case open at a time.", "Open Case",
                                          MessageBoxButtons.OK,
                                          MessageBoxIcon.Stop);
    
                                win.WindowState = System.Windows.WindowState.Normal;
                                win.Focus();
                                win.Activate();
                                return;
                            }
                        }
                    }