C# WPF MVVM Command class not working - ICommand implementation
23:15 20 Apr 2026

I am working on a WPF project using the MVVM pattern. My project is structured into three separate projects/namespaces to maintain a clean separation of concerns:

Model: Contains the data classes (e.g., class.cs)

ViewModel: Contains the Command implementation and the ViewModels (e.g., VMClass.cs, VMMainWindow.cs).

View (Main Project): The XAML UI that references the ViewModel project.

The Issue:

Even though my DataContext is correctly set in XAML and the bindings don't show any errors in the output, the logic inside my Command is never executed.

My Code Implementation:.

Here is my code:

// Inside ViewModel Project
public class Command : ICommand
{
    private Action action;
    public Command(Action action) { this.action = action; }

    public bool CanExecute(object parameter) => true;
    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        // I suspect this is the culprit:
        if (parameter != null)
        {
            action(parameter);
        }
    }
}

My View Setup (XAML):

I have linked the ViewModels as a namespace and set the DataContext like this:


    


My question:

1. Is it bad practice to have this if (parameter != null) check in a generic Command class?

2. How should I handle cases where no CommandParameter is provided?

c#