Search code examples
c#windows-runtimewindows-store-apps

Detect ListViewitem where rightTapped event happened


Is there any way to get the ListItem from RightTappedRoutedEventArgs e without SelectionChanged event and related sources because this ListView have SelectionMode="none". If this is not possible with SelectionMode="none", will it be available with other selection types, but still without selection change event?

Item xaml template below.

<ListView.ItemTemplate>
 <DataTemplate>
   <Grid Height="56" Width="300">
     <Image .../>
    <TextBlock .../>
   </Grid>
 </DataTemplate>
</ListView.ItemTemplate>

Due to some experiments, I have subclassed ListView (with currently unused functionality) and also subclassed ListViewItem with handling RightTapped. Maybe there is any way to attach this to event? Storing this as selection result in subclassed ListView is a not good behavior I think.


Solution

  • <ListView.ItemTemplate>
     <DataTemplate>
       <Grid Height="56" Width="300" IsHitTestVisible="False">
    ...
    

    So now I see always Border as original sender. It was TextBox or Image before.

    then at:

    private void itemsListBoxRightTapped( object sender, RightTappedRoutedEventArgs e )
    {
      Border clickBorder = e.OriginalSource as Border;
      if ( clickBorder != null )
      {
         MyItemType selectedItem = clickBorder.DataContext as MyItemType;
       ...
    

    viola! Tapped item.

    Now correct context menu for ListView is done ;)

    Update: Windows Universal apps have ListViewItemPresenter instead of Border.