Q:

How to create my own custom commands

I am new to the MVVM pattern and I am trying to write my own custom Commands. What is the best method to write custom commands? (I read a lot of articles here and there, but could not obtain the idea of creating my commands). And in particular if someone can help me with the idea of creating a command, after a button is clicked to get the items from one ListView and put them in another. (The list view is bounded to ObservableCollection). Thanks in advance!

A:

Just implement the ICommand interface. There are many on the web or even implemented in nuget packages. Search for RelayCommand or DelegateCommand with ICommand and you will find many implementations. After you have that you just wrap the VMs private method in a command. e.g.
ICommand startTestCommand;

public ICommand StartTestCommand { get { return startTestCommand ?? (startTestCommand = new RelayCommand(StartTest)); }

private void StartTest()
{
    //This will execute if the Command is bound in XAML
}

To bind the command in XAML (assuming your view model is the data context) just bind the Command property of the button to "StartTestCommand" or whatever your command is called.
So in essence you do not "Create your own command" for what you are asking, but wrap the delegate for a Command mediator to handle preventing coupling. There are reasons to create your own commands, but I would recommend using what is out there.

