简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM