public class ViewModel { public RelayCommand AddItemCommand { get; set; } public ViewModel() { AddItemCommand = new RelayCommand(AddItem); } private void AddItem() { // Add item logic goes here } } // In XAML:
public class ViewModel { public ObservableCollectionIn the code examples above, we are using the RelayCommand.Execute method to execute the logic associated with the command. In the first example, we are adding an item to a collection when a button is clicked. In the second example, we are removing an item from a collection when a button is clicked, and passing the item as a parameter to the command. The package library used in these examples is Microsoft.Xaml.Behaviors.Wpf.Items { get; set; } public RelayCommand RemoveItemCommand { get; set; } public ViewModel() { Items = new ObservableCollection () { "Item 1", "Item 2", "Item 3" }; RemoveItemCommand = new RelayCommand (RemoveItem); } private void RemoveItem(string item) { Items.Remove(item); } } // In XAML: