Let's say I currently have an ItemsControl whose DataTemplate is a bunch of buttons. I'm wiring up these buttons' click events, but how am I to know which button was clicked? Should I not use a ItemsControl?
I'm trying to have no code-behind, but being pragmatic may be necessary.
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Margin="10">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding ItemsControlButtonClicked, Mode=OneWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
In my project I also use the MVVM Light I has an dropdown with collection of items, and a button which user press and action depend on selected item from drop down you should create a Relay command with parameter look at the example from my code
on the view I bind the drop down with collection of class Project and for button command parameter I bind the selected item form drop down look at the code
pay attention to Command and CommandParameter binding
also you can use this approache not only for drop down
Create command properties in your view model class (using Josh Smith's
RelayCommand
pattern is the simplest way to do this) and bind each button'sCommand
to the appropriate one. Not only is this straightforward to do and simple to maintain, it also gives you an easy way of implementing the enable/disable behavior when you need to.Well, you can use the Sender.DataContext which is the actual data.
You can send parameters along with the command and based on these parameters you can find out which button was clicked
If you want to know what
Item
was clicked, then pass{Binding }
as theCommandParameter
and it will pass the selected object to your CommandIf you want to know what
Button
was clicked, I would do that in the code-behind since ViewModels do not need to know anything about the UI, and that includes buttons.Also since your control is a Button, you should use the
Command
property instead of a Click trigger.