can I use lamda tothe ICommand in wpf?

mc 5,776 Reputation points
2025-08-15T06:40:09.3+00:00

I am using mvvm in wpf and I wonder can I use this :

public ICommand MyCommand{get;}

public MainViewModel()
{
      MyCommand=(o,e)=>
       {
        };
}

and there will be error

and I have to use public MyRelayCommand:ICommand {} ?

Developer technologies | Windows Presentation Foundation
0 comments No comments
{count} votes

Accepted answer
  1. Susmitha T (INFOSYS LIMITED) 575 Reputation points Microsoft External Staff
    2025-08-18T07:20:37.3733333+00:00

    Hope you are doing good! Thank you for reaching out. Please find the answer below.

    It sounds like you're trying to work with the ICommand interface in WPF while implementing the MVVM pattern.
    The issue you're facing appears to be related to the incorrect way you are defining or using your ICommand. To clarify, you need a class that implements ICommand. A common pattern is to use a RelayCommand (or DelegateCommand) that wraps your lambda logic.

     

    1. Create a RelayCommand class: First, you would need to create a class that implements ICommand. This is often referred to as a RelayCommand or DelegateCommand. Here's a quick example:

     

    public class RelayCommand : ICommand
    {    
    private readonly Action<object> _execute;    
    private readonly Predicate<object> _canExecute;

     
    public RelayCommand(Action<object> execute, Predicate<object> canExecute = null)  
       {       
      _execute = execute ?? throw new ArgumentNullException(nameof(execute));        
    _canExecute = canExecute;    
    }

     public event EventHandler CanExecuteChanged   
      {        
    add { CommandManager.RequerySuggested += value; }        
    remove { CommandManager.RequerySuggested -= value; }  
       }

       public bool CanExecute(object parameter) => _canExecute == null || _canExecute(parameter);

      public void Execute(object parameter) => _execute(parameter);
    }  

    2. Use it in your ViewModel:

     public ICommand MyCommand { get; }

     public MainViewModel()
    {    
    MyCommand = new RelayCommand(ExecuteMyCommand, CanExecuteMyCommand);
    }

    private void ExecuteMyCommand(object parameter)
    {    
    // Your command logic here

    }

    private bool CanExecuteMyCommand(object parameter)
    {
        // Logic to determine if the command can execute   
      return true; // or some condition
    }
    And in your XAML, you can bind it like this:

    <Button Content="Execute command" Command="{Binding MyCommand}" />

     

    This way, your command will correctly implement the Execute and CanExecute methods required by the ICommand interface, and your MainViewModel should work without errors.

     

    If issue still persist after following all the steps, we’ll be happy to assist further if needed." Kindly mark the answer as accepted if the issue resolved".

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Starry Night 110 Reputation points
    2025-08-18T07:21:27.9933333+00:00

    public MainViewModel() { MyCommand=(o,e)=> { }; }

    If we use the code above you shared in wpf, we could encounter the following error tip as you mentioned:

    User's image

    This error occurs if you try to assign or otherwise convert an anonymous method block to a type which is not a delegate type. Refer: Compiler Error CS1660 .

    According to your description, do you want to use the Command as follows ,right?

                MyCommand = new Command<string>(fooObject =>
                {
                    //add your code here, fooObject should have the passed value  
                });
    

    But in maui, the class Command is a sealed class which has implemented the ICommand interface.User's image

    And in wpf, we don't have such a class which has implemented the ICommand interface.

    So if we want to use mvvm, we need to implement ICommand interface ourself which acts as Enhancement for the ICommand and extracts the boiler plate code to a separate class.

    For how to use the ICommand Interface In MVVM - WPF, you can refer document : ICommand Interface In MVVM - WPF.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.