UWP 中 ListView 项目的 DoubleTapped 事件

DoubleTapped event for ListView items in UWP

我希望能够双击列表视图项目。我使用了 DoubleTapped 事件,但是当我双击列表视图(不是项目)事件时,我只想处理项目(而不是列表视图本身(面板、边框等)) ,在 wpf 中我们可以通过创建 listviewitem 样式来简单地做到这一点:

<Style TargetType="ListViewItem">
            <EventSetter Event="MouseDoubleClick" Handler="ListViewItem_MouseDoubleClick" />
        </Style>

但此方法在 uwp 中不起作用,我该怎么做?

I want to be able to double tap on listview items. i used DoubleTapped event but when i double tap on listview (not items) event is fire, i only want to work with items (not listview itself (panels, borders,...)),

UWP平台不支持添加EventSetter,如果要为ListViewItem添加DoubleTapped,请添加到ItemTemplate

例如

<ListView VerticalAlignment="Bottom">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid DoubleTapped="Grid_DoubleTapped">
                <TextBlock Text="{Binding}" />
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

如上面的代码,您可以监听Grid DoubleTapped,请注意我们需要将ListViewItem HorizontalContentAlignment设置为Stretch以确保整个项目可以响应双击。