Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my model :
C#
class Login
    {
        public string Name{ get; set; }
        public string Password { get; set; }
    }

my view model :
XML
class LoginViewModel : ObservableObject, INotifyPropertyChanged, ICommand
   {
       class LoginViewModel
       {
           Models.Login _mylogin = new Models.Login();
       }
   }

C#
private readonly Models.Login _mylogin;
        public string Name
        {
            get { return _mylogin.Name; }
            set { _mylogin.Name= value; NotifyPropertyChanged("Name"); }
        }
        public string Password
        {
            get { return _mylogin.Password; }
            set { _mylogin.Password = value; NotifyPropertyChanged("Password"); }
        }


C#
public event PropertyChangedEventHandler PropertyChanged;
       protected void NotifyPropertyChanged(string info)
       {
           if (PropertyChanged != null)
           {
               PropertyChanged(this, new PropertyChangedEventArgs(info));
           }
       }



Question: What is the easiest way to implement icommand ? . this is my first wpf example with MVVM patern . i would like to know how command works.(sorry for my poor english)
Posted

Some good support for MVVM can be found in Josh Smith's MVVM Foundation library[^].
There is an implementation of RelayCommand and other quite useful stuff!
 
Share this answer
 
You definitely should do more reading. But to get you started...

You don't want your VM to implement ICommand. Commands are separate objects exposed by your VM. To avoid creating a class for each command a common implementation named DelegateCommand or RelayCommand is used. You can implement one yourself or use one which comes with a MVVM framework (e.g. Prism, some more links here). As the name suggest the command will relay the call to a delegate you provide in the constructor. Here is how it works:

C#
class LoginViewModel 
{
  ICommand MyCommand { get; private set; }

  public LoginViewModel()
  {
    MyCommand = new DelegateCommand(MyMethod);
  }

  private void MyMethod(object parameter)
  {
    // handle the call here
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900