简体   繁体   中英

Button Command won't close window

I have two windows that are both open. I am trying to be able to click a button on one window and that should close that same window. I am looking to do this with button commands and a command parameter. The execute method switches through the command parameters to find which button is clicked and should perform the specified task accordingly like so:

public void Execute(object parameter)
    {
        LoginPage loginPage = new LoginPage();
        CreateAccountPage createAccountPage = new CreateAccountPage();
        DepositPage depositPage = new DepositPage();
        WithdrawPage withdrawPage = new WithdrawPage();

        switch (parameter)
        {
            case "LoginPageButton":
                if (viewModel.IsLoggedIn == false)
                loginPage.Show();
                else if (viewModel.IsLoggedIn == true)
                        loginPage.Close();
                break;
            case "CreateAccountPageButton":
                createAccountPage.Show();
                break;
            case "DepositPageButton":
                depositPage.Show();
                break;
            case "WithdrawPageButton":
                withdrawPage.Show();
                break;
            case "ClearOrderButton":
                //code here
                break;
            case "LogoutButton":
                //code here
                break;
            case "FinishLoginButton": // this is where the issue is
                loginPage.Close();//this does not close the loginPage window
                break;
        }
     }

There are no build errors and the command will execute other tasks like opening other windows and etc but will not close the window so I know the code is reached, the window just will not close and after days of research I haven't been able to find an answer. I am able to perform the desired task in a button click like so:

private void FinishLogin_Click(object sender, RoutedEventArgs e)
    {
        if ((viewModel.usernameList.Contains(UsernameTextBox.Text) &&
           viewModel.passwordList.Contains(PasswordTextBox.Text)) &&
           (viewModel.usernameList.IndexOf(UsernameTextBox.Text) ==
           (viewModel.passwordList.IndexOf(PasswordTextBox.Text))))
        {
            viewModel.IsLoggedIn = true;
            this.Close();
        }
    }

along with the future tasks I need to perform, but I do not want to use a button click, and I will need inotifypropertychanged in the future so I need the command. This is the code for the button that needs to be clicked to close the window:

<Button Content="Finish Login" Grid.Column="1" Grid.Row="2"     FontWeight="Bold" Margin="130 10 30 10" Background="Firebrick" Command="{Binding ButtonCommands}" CommandParameter="FinishLoginButton"/>

I know a lot of this code will not follow MVVM, but I am just looking to make this code work at the moment.

In this example, your creating a new instance LoginPage loginPage = new LoginPage(); then closing that particular instance. What you need to do is pass the existing instance of your window and close that.

In your Window code-behind (if you are setting the viewmodel this way):

//Pass the current instance of the window to the viewmodel
this.DataContext = new LoginPage(this); 

Then, update the constructor of your viewmodel to take the instance of the window

private Window _parent;
public LoginPage(Window Parent)
{
    _parent = Parent;
}

And update your Execute method (removing the new LoginPage() etc)

public void Execute(object parameter)
{
    [....]
    _parent.Close();
    [....]
}

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