WPF. How hide/show main window from another window

爷,独闯天下 提交于 2019-12-07 03:11:23

问题


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?


回答1:


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();
}



回答2:


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;
}



回答3:


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();
   }
}



回答4:


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!



来源:https://stackoverflow.com/questions/6814170/wpf-how-hide-show-main-window-from-another-window

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!