问题
I have a GridView where I'm storing the songs the user adds to a collection:
<GridView x:Name="MusicGridView" Margin="10" IsItemClickEnabled="True" ItemClick="Play_Song" Grid.Row="0" ItemsSource="{Binding Songs, ElementName=thisPage, Mode=OneWay}" SelectionMode="None">
<GridView.ItemTemplate>
<DataTemplate>
<Grid Height="100" Width="100" RightTapped="Song_RightTapped">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Rectangle Grid.Row="0" Grid.RowSpan="2" Fill="#FF1F4E79"/>
<Image Source="{Binding Path=AlbumArt.Source}" Grid.Row="0" Height="40" Width="40" Margin="10,10,10,0"/>
<TextBlock Text="{Binding Title}" Foreground="White" VerticalAlignment="Bottom" HorizontalAlignment="Center" Grid.Row="1" Margin="5" TextWrapping="Wrap"/>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
When the Grid is right-tapped (I can't seem to set a RightTapped property for the actual item) I show a PopupMenu. However, the sender is the Grid itself, of course, and I can't find a way to get the actual GridViewItem without doing what seem like really weird workarounds. Getting the selected item of the GridView isn't an option, since selection isn't enabled on it. Is there an easy way to get the item as a GridViewItem?
回答1:
If I understand your question correctly you want to get the binded object instead of the GridView. If that's the case you could try this.
private void Song_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
Song song = (sender as Grid).DataContext as Song;
// Show PopupMenu
// ...
}
来源:https://stackoverflow.com/questions/32927743/get-righttapped-gridviewitem