nerd
nerd

Reputation: 369

double tap on list view

When I double tap on a list view item I am getting the DoubleTapped event.

But I am not sure how to get the selected item on which the tap was performed. ListView.Selecteditem does not give me the tapped item.

Please help.

Upvotes: 1

Views: 2259

Answers (3)

nerd
nerd

Reputation: 369

I have found a way to solve this.

When a listview item is tapped before getting the DoubleTap event , you will get GetFocus event. In that event you will get the selected item and you can use this selected item in Doubletap.

private async void OnDoubleClick(object sender, 
Windows.UI.Xaml.Input.DoubleTappedRoutedEventArgs e)    
{

}
private void OnFocus(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
ListviewItem lv = (ListViewItem)e.OriginalSource;
string str = lv.SelectedItem.tostring();
}

Thanks

Upvotes: 3

michalczukm
michalczukm

Reputation: 10459

DataGrid and ListView had nice method HitTest, by which you can get selected item.

private void ListView_Tapped(object sender, TappedRoutedEventArgs e)
    {
        var listView = sender as ListView;
        if (!(sender is ListView))
        {
            return;
        }

        var hitTest = listView.HitTest(e.X, e.Y);

        ListViewItem tappedListViewItem = hitTest.Item;
    }

Upvotes: 2

letiagoalves
letiagoalves

Reputation: 11302

As Aaron Xue said here, you can't get the clicked item via Tapped event directly. However you can get the Y coordinate and calculate the item index we clicked then get the item:

private void ListView_Tapped(object sender, TappedRoutedEventArgs e)
{
    int item = 0;
    Double coY = e.GetPosition((UIElement)sender).Y;

    ListView lv = sender as ListView;
    if (sender is ListView)
    {
        lv.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
        Size lvSize = lv.DesiredSize;
        item = (int)(coY / lvSize.Height * lv.Items.Count);
        item = item > lv.Items.Count ? lv.Items.Count : item;
    }

    var TappedItem = lv.Items[item];
}

Upvotes: 1

Related Questions