private bool CanSave(object parameter) { if (string.IsNullOrWhiteSpace(textbox1.Text)) return false; else return true; } private void Save(object parameter) { // Save the data } // In the constructor of the viewmodel saveCommand = new RelayCommand(Save, CanSave);
private bool CanRemove(object parameter) { if (selectedItem == null) return false; else return true; } private void Remove(object parameter) { // Remove the selected item } // In the constructor of the viewmodel removeCommand = new RelayCommand(Remove, CanRemove);In this example, we define a command called removeCommand, which will execute the Remove method when a menu item is clicked. We also define a method called CanRemove, which returns true if an item is selected in the UI, and false otherwise. Package Library: Microsoft.Toolkit.Mvvm. This library contains various classes and methods for creating MVVM (Model-View-ViewModel) applications using C#. RelayCommand is one of the classes in this library that implements ICommand interface for command bindings.