I would like to add a command to my listview. When an item is clicked I would like to execute my relayCommand. Now I have these solution, it works, but I think it is not so MVVM :)
<ListView ItemsSource="{Binding Taxons}" IsItemClickEnabled="True" ItemClick="ListViewBase_OnItemClick">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Code Behind:
private void ListViewBase_OnItemClick(object sender, ItemClickEventArgs e)
{
viewModel.TreeItemSelected.Execute(((Taxon)e.ClickedItem).Name);
}
I would like to use this way without any code behind, but it is not possible as the VS tells me:
<ListView ItemsSource="{Binding Taxons}" IsItemClickEnabled="True" ItemClick="{Binding TreeItemSelected}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I use the following extra dll-s, but i dont want to install anything else if it possible. GalaSoft.MvvmLight.Extras.Win8 GalaSoft.MvvmLight.Win8
Can you suggest a solution to me?
Here's a working example :
<ListBox x:Name="name_here"
ItemsSource="{Binding your_item_source}"
SelectedItem="{Binding your_selected_item, UpdateSourceTrigger=PropertyChanged}"
...
>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<Command:EventToCommand Command="{Binding your_command_here}"
CommandParameter="{Binding ElementName=name_here, Path=SelectedItem}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>
It make use of Blend's Interaction triggers (you don't have to install blend, the dll is enough if you don't have it).
also, if you're using the CommandParameter
, bind the ElementName
to the name of the control (in this example it's name_here
)