Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
My project has a viewmodel class in which I have inherited RelayCommand and ICommand.

A Command called ExecuteCommand is created in this view model as shown below and is working fine.

public class LoginWindowViewModel:WorkSpaceViewModel
    {  
        RelayCommand _executecommand;        
        public ICommand ExecuteCommand
        {
            get
            {
                if (_executecommand == null)
                {
                   _executecommand = new RelayCommand(param => Execute(),param => CanExecute());
                }
                return _executecommand;
            }
        }        

        public virtual void Execute()
        {            
            MainWindow window = new MainWindow();
            window.Show();            
        }

        public virtual bool CanExecute()
        {
            return true;
        }
    }

My question is, how can I create another command in the same view model and how to use it.
Posted

1 solution

Simple. Just create another ICommand property:


C#
RelayCommand _executecommand2;
public ICommand ExecuteCommand2
{
    get
    {
        if (_executecommand2 == null)
        {
            _executecommand2 = new RelayCommand(param => Execute2(), param => CanExecute2());
        }
        return _executecommand2;
    }
}

public virtual void Execute2()
{
    MainWindow window = new MainWindow();
    window.Show();
}

public virtual bool CanExecute2()
{
    return true;
}

For using it, you can just bind the ICommand property to the Command property of your view's element. Something like:


XML
<Button Command="{Binding ExecuteCommand2}" />
 
Share this answer
 
v2
Comments
Rahul Krishnan R 17-Mar-13 8:07am    
Thanks, It Helped.

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