Hi,
I am a beginner when it comes to MVVM. I am creating a simple Login page which takes user name and password. As usual if user name and password matches it should open a new window and if user name and password does not matches it should show a message box. Well i think i can accomplish it by creating object of new window and then by calling Show() method in the ViewModel class, but i don't think it is right approach using MVVM. So please suggest what i need to do so that my Login Page should follow pure MVVM.
XAML code<Window x:Class="WpfApplication1.View.LoginWin" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:viewModel="clr-namespace:WpfApplication1.ViewModel" Title="LoginWin" Height="300" Width="300"><Window.DataContext><viewModel:LoginViewModel></viewModel:LoginViewModel></Window.DataContext><Grid><Grid><TextBox Height="23" HorizontalAlignment="Left" Margin="96,74,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Text="{Binding Path=UserName,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" /><TextBox Height="23" HorizontalAlignment="Left" Margin="96,113,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" Text="{Binding Path=Password,UpdateSourceTrigger=PropertyChanged}" /> </Grid><Button Content="Login" Height="23" HorizontalAlignment="Left" Margin="103,149,0,0" Name="button1" VerticalAlignment="Top" Width="75" Command="{Binding LoginCommand}" /></Grid></Window> LoginViewModel.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Input; using WpfApplication1.Commands; namespace WpfApplication1.ViewModel { public class LoginViewModel:ViewModelBase { private string _userName; private string _password; private ICommand _loginCommand; public string UserName { get { return _userName; } set { _userName = value; OnPropertyChanged("UserName"); } } public string Password { get { return _password; } set { _password = value; OnPropertyChanged("Password"); } } public ICommand LoginCommand { get { if (_loginCommand == null) { _loginCommand = new RelayCommand(new Action<object>(CheckLogin), new Predicate<object>(CanUserLogin)); } return _loginCommand; } } private void CheckLogin(object o) { if (UserName == "abc" && Password == "123") { //stuff to open new window } else { //stuff to show a message box for incorrect login credentials } } private bool CanUserLogin(object sender) { return !(string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password)) ; } } } RelayCommand.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Input; namespace WpfApplication1.Commands { public class RelayCommand:ICommand { #region Class Variables private readonly Predicate<object> _canExecute; private readonly Action<object> _execute1; #endregion #region Constructor public RelayCommand(Action<object> execute) : this(execute, null) { } public RelayCommand(Action<object> execute, Predicate<object> canExecute) { if (execute == null) throw new ArgumentNullException("execute"); _execute1 = execute; _canExecute = canExecute; } #endregion public bool CanExecute(object parameter) { return _canExecute == null ? true : _canExecute(parameter); } public event EventHandler CanExecuteChanged { add { if (_canExecute != null) { CommandManager.RequerySuggested += value; } } remove { if (_canExecute != null) { CommandManager.RequerySuggested -= value; } } } public void Execute(object parameter) { _execute1(parameter); } } }