我已经知道如何使用ItemClick事件在listview中获取项目。 查看此主题: 在ListView Windows Phone中获取项目
现在,我也想使用RightTapped获取我的列表视图中的项目。
感谢你的帮助。
谢谢!
I already know how to get item in listview using ItemClick event. See this thread: Get Items in ListView Windows Phone
Now, I also wanted to get the item in my listview using RightTapped.
Appreciate your help.
Thanks!
最满意答案
对于RightTapped事件,您可以接收RightTappedRoutedEventArgs对象。 从这里你可以得到正确的ListViewItemPresenter ,你应该可以通过它的DataContext获取该项目。 代码如下:
private void listViewDirectory_RightTapped(object sender, RightTappedRoutedEventArgs e) { ListViewItemPresenter itempresenter = e.OriginalSource as ListViewItemPresenter; Person obj = (Person)itempresenter.DataContext; Debug.WriteLine(obj.Name); }如果你不想使用ListViewItemPresenter , FrameworkElement也可以工作。
FrameworkElement itempresenter = e.OriginalSource as FrameworkElement; Person obj = (Person)itempresenter.DataContext; Debug.WriteLine(obj.Name);For RightTapped event you can receive RightTappedRoutedEventArgs object. From this you can get the right tapped ListViewItemPresenter and you should be able get the item by its DataContext. Code as follows:
private void listViewDirectory_RightTapped(object sender, RightTappedRoutedEventArgs e) { ListViewItemPresenter itempresenter = e.OriginalSource as ListViewItemPresenter; Person obj = (Person)itempresenter.DataContext; Debug.WriteLine(obj.Name); }If you don't want use ListViewItemPresenter, FrameworkElement also worked.
FrameworkElement itempresenter = e.OriginalSource as FrameworkElement; Person obj = (Person)itempresenter.DataContext; Debug.WriteLine(obj.Name);更多推荐
发布评论