When using mvvmlight, I bind a tap event to the listboxitem of ListBox.
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP71"
<ListBoxItem> <StackPanel> <i:Interaction.Triggers> <i:EventTrigger EventName="Tap"> <cmd:EventToCommand Command="{Binding TapCommand}"></cmd:EventToCommand> </i:EventTrigger> </i:Interaction.Triggers> <TextBlock Text="11"></TextBlock> <TextBlock Text="222"></TextBlock> </StackPanel></ListBoxItem>
public ICommand TapCommand { get; set; }TapCommand = new RelayCommand(Tapped);void Tapped(){ MessageBox.Show("test");}
In this way, you can listen to the TAP event.
But now I want to use a template to bind data to the background.
The template is as follows:
<ListBox ItemsSource="{Binding Path=UserList}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <i:Interaction.Triggers> <i:EventTrigger EventName="Tap"> <cmd:EventToCommand Command="{Binding TapCommand}"></cmd:EventToCommand> </i:EventTrigger> </i:Interaction.Triggers> <TextBlock Text="{Binding id}"></TextBlock> <TextBlock Text="{Binding name}"></TextBlock> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
At this time, the tap page cannot monitor the tap event. How can this problem be solved?