WPF How to hide / show the main window from another window

I have two windows MainWindow and Login. A button that shows the login located on mainWindow

this.Hide();
        Login li = new Login();
        li.Show();

in the login window - a button that verifies the password, how can I show MainWindow if the password is correct?

+5
source share
4 answers

pass parameter to loginwindow of type MainWindow. This allows the Login window to have a link to MainWindow:

this.Hide();
Login li = new Login(this);
li.Show();

And the login window:

private MainWindow m_parent;
public Login(MainWindow parent){
    m_parent = parent;
}

//Login Succesfull function

private void Succes(){
    m_parent.Show();
}
+7
source

the first answer is good, but it will create a new blank window to avoid this problem (redirect to a previously created window) just change the constructor like this

 public Login(MainWindow parent):this()
{
    m_parent = parent;
}
+3
source

....

this.Hide();
Login li = new Login();
if(li.ShowDialog() == DialogResult.OK){
   //Do something with result
   this.Show();
}

, - ...

void OnLogin(){
   if(ValidateLogin()){
      this.DialogResult = DialogResult.OK;
      this.Close();
   }
}
+2

What kind of layout, etc. do you use for your user interface? If you make the logon window a modal dialog, do you need to hide the main window?

Alternatively, you can have some kind of flag "successfully logged in" and bind the visibility of each window to this value - using converters to get the desired result? Sort of:

<Grid>
    <MainWindow Visibility="{Binding Authorized,
                      Converter={StaticResource BoolToVisibilityConverter}}"/>

    <LoginWindow Visibility="{Binding Authorized,
                Converter={StaticResource InvertedBoolToVisibilityConverter}}"/>
</Grid>

It makes sense?

EDIT: Obviously, elements in a Grid cannot be Windows - so my initial question is about the layout used!

0
source

All Articles