获取 RightTapped GridViewItem [英] Get RightTapped GridViewItem

查看:18
本文介绍了获取 RightTapped GridViewItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 GridView 用于存储用户添加到集合中的歌曲:

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>

当右键点击 Grid 时(我似乎无法为实际项目设置 RightTapped 属性)我显示一个 PopupMenu.然而,发送方当然是 Grid 本身,我无法找到一种方法来获取实际的 GridViewItem 而不做一些看起来非常奇怪的解决方法.获取 GridView 的选定项目不是一个选项,因为它没有启用选择.有没有一种简单的方法可以将项目作为 GridViewItem 获取?

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?

推荐答案

如果我正确理解您的问题,您希望获得绑定对象而不是 GridView.如果是这样,你可以试试这个.

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
    // ...
}

这篇关于获取 RightTapped GridViewItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆