WP8 Tap the image to get the selected item in longlistselector

前端 未结 1 1676
Happy的楠姐
Happy的楠姐 2020-12-20 04:36

I created a LongListSelector with textblock and an image,then How do I tap the image to show selected item name in my list, and tap the item name to show messagebox? Below i

相关标签:
1条回答
  • 2020-12-20 05:34

    sender isn't the LongListSelector but the image on which the user tapped, hence the null error.

    Basically, you just want to retrieve the item on which the user has tapped? In that case, use the DataContext property of the tapped control to retrieve it:

    private void GetName_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
       var element = (FrameworkElement)sender;
       StaffData data = (StaffData)element.DataContext;
       MessageBox.Show(data.Name);
    }
    

    (FrameworkElement is the base type of every UI control. Using that, you don't have to worry about whether it's an image, a textblock, ...)

    0 讨论(0)
提交回复
热议问题