Search code examples
.netxamllistviewuwpwinui

DoubleTapped event for ListView items in 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,...)), in wpf we can simply do this by creating listviewitem style:

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

but this method not worked in uwp, how can i do this?


Solution

  • 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 platform does not support add the EventSetter, If you want to add DoubleTapped for ListViewItem, please add it into ItemTemplate.

    For example

    <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>
    

    As the code above, you could listen the Grid DoubleTapped, please note we need set ListViewItem HorizontalContentAlignment as Stretch to make sure the whole item could response double tap.