0

in my project i have a login page named login.xaml and i had loginViewModel.cs for using MVVM Approach.. At beginning i wrote this.dialogResult=true in my code-behind page(login.xaml.cs) and use the code means it closes the childwindow.. here i need to close the childwindow(login.xaml) from viewmodel(loginviewmodel).

login.xaml:

private void btnLogin_Click(object sender, RoutedEventArgs e)
        {

            if (txtuser.Text.Trim() != "" && txtpass.Password != "")
            {
                (DataContext as LoginViewModel).UserValidation(txtuser.Text.Trim(),txtpass.Password.Trim());
            }
        }

loginviewmodel.cs:

public void UserValidation(string name, string pass)
        {
            IsBusy =true;
            uname=name;
            pword=pass;
            // ----* (Follow * for the continuation )

        }

*--> here i need to close the childwindow.. how to close it..

Jasper Manickaraj
  • 797
  • 3
  • 12
  • 35
  • See [wpf--mvvm-newbie-how-should-the-viewmodel-close-the-form](http://stackoverflow.com/questions/501886/wpf-mvvm-newbie-how-should-the-viewmodel-close-the-form/3329467#3329467) Gives a discussion of various ways of doing it – SteveL Feb 26 '13 at 10:30

1 Answers1

3

I got the same problem and solved it... So I have my Child Window and the button Cancel:

<Button x:Name="CancelButton" Content="Cancel" Command="{Binding CancelCommand}" CommandParameter="{Binding ElementName=SignUpPopup}" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1"/>

and what I do is I pass the object Child Window - that has Name="SignUpPopup" trough the ExecuteCancelCommand parameter, param. and in the view model you have:

public void ExecuteCancelCommand(object param)
        {
            (param as Signup).Close();
           // MessageBox.Show("Window should close now");
        }

Signup is the child window type. Hope this helps,

Vlad

Vlad
  • 536
  • 1
  • 5
  • 18