Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Phone 8.1 select an item in a listview by holding

is there a way to select an item in a listview with the holding event in Windows Phone 8.1?

If I use a single tap, I use this code and it works fine

private void LstMyListView_Tapped(object sender, TappedRoutedEventArgs e)
{
  MyItem myItem = LstMyListView.SelectedItem as MyItem;
}

myItem contains the data of my selected item

However, if I use this code

private void LstMyListView_Holding(object sender, HoldingRoutedEventArgs e)
{
  MyItem myItem = LstMyListView.SelectedItem as MyItem;
}

myItem results as null.

How do I get the selected item with hold event??

Thank you!

like image 624
Ravenheart Avatar asked Jan 10 '23 10:01

Ravenheart


1 Answers

You should be able to retrive your item from DataContext and perform a cast, for example like this:

private void LstMyListView_Holding(object sender, HoldingRoutedEventArgs e)
{
    FrameworkElement element = (FrameworkElement)e.OriginalSource;
    if (element.DataContext != null && element.DataContext is MyItem)
    {
        MyItem selectedOne = (MyItem)element.DataContext;
        // rest of the code
    }
}
like image 127
Romasz Avatar answered Jan 29 '23 12:01

Romasz