Reputation: 929
I was created a LongListSelector with textblock and image,then I click the title to show selected staff name, and click image to show another message box. If I click on name, the message box is displayed successful. When I clicked on image,the message box for image, is displayed successful,but the message box for staff name displayed as well. How to I solve this issue?
I am by using the code below:
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="TileDataTemplate">
<Grid Background="{StaticResource TransparentBrush}"
Margin="0, 0, 0, 12" Height="60">
<TextBlock Text="{Binding Name}" Margin="60, 10, 0, 0" FontSize="24" Height="60">
</TextBlock>
<Image x:Name="delete" Tap="delete_Tap" Grid.Column="0" Source="/Assets/AppBar/Delete.png" Height="40" Width="40"
Margin="0, 6, 0, 5" HorizontalAlignment="Right" VerticalAlignment="Top" />
</Grid>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<phone:LongListSelector
SelectionChanged="MainLongListSelector_SelectionChanged"
Margin="10,6,0,0"
ItemsSource="{Binding Staff.Items}"
LayoutMode="Grid"
GridCellSize="400,80"
ItemTemplate="{StaticResource TileDataTemplate}"
/>
</Grid>
The result as print screen below:
Code Behind:
private void MainLongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
LongListSelector selector = sender as LongListSelector;
StaffData data = selector.SelectedItem as StaffData;
MessageBox.Show(data.Name);
}
private void delete_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
MessageBox.Show("delete?");
}
Please teach me how to solve this issue. Thanks.
Upvotes: 1
Views: 513
Reputation: 89
sender isn't the LongListSelector but the image on which the user tapped, hence the null error.
Basically, you just want to retrieve the item on which the user has tapped? In that case, use the DataContext property of the tapped control to retrieve it:
private void GetName_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
var element = (FrameworkElement)sender;
StaffData data = (StaffData)element.DataContext;
MessageBox.Show(data.Name);
}
Upvotes: 0
Reputation: 3526
You'll want to stop propagation of the event, so do e.Handled = true;
in the handler you want to have the event stop at.
From the docs about Handled
:
"Gets or sets a value that marks the routed event as handled. A true value for Handled prevents most handlers along the event route from handling the same event again."
Upvotes: 1