MVVM Light has a common RelayCommand class that can handle command parameters. The non-generic RelayCommand class ignores them. Here is an example:
Bind the CommandParameter property first, as Ahmed showed in his answer:
<Button Content="{Binding}" Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}, Path=DataContext.CategorySelectedCommand}" CommandParameter="{Binding}" />
Lets say the linked list for buttons in your viewmodel is a list of strings. The contents of your button and your CommandParameter are tied to one of your list items. Now create a generic RelayCommand repeater in your view model:
private RelayCommand<String> mOnClickCommand; public RelayCommand<String> ClickCommand { get { return mOnClickCommand; } }
Create it like this, for example, in your constructor:
mOnClickCommand = new RelayCommand<string>(OnButtonClicked);
And finally, in your method, you will get the CommandParameter property as an argument to the method:
private void OnButtonClicked(String _category) { Console.WriteLine(_category); }
Hope this will be helpful.
source share