Search code examples
c#wpfinstanceshow-hide

Hide an Instance of a Window


how do I hide an Instance of a Window which was created in an Method, in another Method?

I have a Login Window with a button "Register" and when you click that, the Register-Window is opening, and the Login-Window is hiding (That is working fine). The Problem now is, i have on the Register-Window a Button "Back" which should hide the Register Window and Show the Login Window. I Can show the Login Window with : "Application.Current.MainWindow.Show();", but i dont know how to hide the Register-Window. Normally i would just go for "Regis.Hide()", but i can´t do that because i have the instance of Regis in the Method, which opens the Register Window.

I understand, that if i create a second instance in the second Method and hide this, that that wont work because there are 2 seperate instances now. But as i said, i dont know how i should do this either.

OpenRegistrationGui:

ICommand _RegisterBack;
public void Open()
{
    Application.Current.MainWindow.Hide();
    Register regis = new Register();
    regis.Show();
}

public ICommand RegisBackCommand
    {
        get
        {
            if (_RegisterBack == null)
            {
                _RegisterBack = new RelayCommand(
                    param => Back()
                    );
            }
            return _RegisterBack;
        }
    }

public void Back()
{
    Application.Current.MainWindow.Show();
    //I Want to Hide the Regis here, but i cant use the Instance from above.
}

MyViewModel:

OpenRegistrierungsGUI RegisGUI = new OpenRegistrierungsGUI();
public ICommand RegisBack
    {
        get => RegisGUI.RegisBackCommand;
        set => RegisBack = RegisGUI.RegisBackCommand;
    }

The Same Thing is with the Open() Method.

The Result i want is simply that the Register Window is hiding and the Login Window shows off.


Solution

  • I found the solution:

    I fired the Open() Method (Which worked perfectly). When I wanted to fire the Back() Method I accidentally created an Instance of "OpenRegistrationGui" and tried to Close it, which it obviousely can´t, because its another Instance. I made the Property "RegisBackCommand" Static and fired it now directly from the ViewModel, which works now.

    Anyway... Thank you all for your fast Answers and help!!!