简体   繁体   中英

WPF. How hide/show main window from another window

I have Two windows MainWindow and Login. The button which shows login located on mainWindow

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

on Login Window is button which checks password how i can show MainWindow if password is correct?

pass a parameter to the loginwindow of type MainWindow. That allows the Login window to have a reference to the 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();
}

first answer is good but it'll create a new empty window to avoid this problem ( redirect to a previously created window) just modify constructor like this

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

What about....

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

Make sure in your Login you have something like...

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

What sort of layout, etc are you using for your UI? If you make the log in window a modal dialog then do you need to hide the main window?

Alternatively, you could have some sort of 'successfully logged in' flag and bind the visibility of each window to this value - using converters to get the desired result? Something along the lines of:

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

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

Does that make sense?

EDIT: Obviously the elements within the Grid can't actually be Windows - hence my initial question about the layout you are using!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM