locked
[UWP] How can I get the clicked item from the RightTapped event on a ListView? RRS feed

  • Question

  • Is there a way to get the item in a ListView that is under the mouse when the RightTapped event is fired?

    In the RightTappedEventArgs I can see (via a breakpoint and inspecting) that the OriginalSource's DataContext contains the clicked item, but I can't seem to access that in my code (e.OriginalSource.DataContext doesn't appear to be a thing).

    For a tap/left click I can just get the clicked item easily, but is there a way to do this with a right-click?

    My intention is to have a right-click context menu that has a couple of actions that will require the object that was clicked (for example I might want to delete the item clicked or share its content to another app).


    Visit http://blog.grogansoft.com/ for Windows development fun.

    Friday, December 25, 2015 10:33 AM

Answers

  • OK, thanks for the help, I have it working now.
    What I did was handle the right-click in the datatemplate, and then the following code gets me the actual item that was right-clicked:
         

       private void StackPanel_RightTapped(object sender, RightTappedRoutedEventArgs e)
            {
                var s = (FrameworkElement) sender;
                var d = s.DataContext;
            }


    Visit http://blog.grogansoft.com/ for Windows development fun.

    • Marked as answer by pumpkinszwan Friday, December 25, 2015 11:57 PM
    Friday, December 25, 2015 11:57 PM
  • Like that, it would be pretty complicated, you would have to search through the visual tree. But if you have righttapped inside the datatemplate, cast sender to FrameworkElement and use its DataContext.

    Check my apps: Share to Speech and File Cards




    • Edited by ivan.icin Friday, December 25, 2015 1:32 PM
    • Marked as answer by pumpkinszwan Friday, December 25, 2015 11:57 PM
    Friday, December 25, 2015 1:28 PM

All replies

  • Like that, it would be pretty complicated, you would have to search through the visual tree. But if you have righttapped inside the datatemplate, cast sender to FrameworkElement and use its DataContext.

    Check my apps: Share to Speech and File Cards




    • Edited by ivan.icin Friday, December 25, 2015 1:32 PM
    • Marked as answer by pumpkinszwan Friday, December 25, 2015 11:57 PM
    Friday, December 25, 2015 1:28 PM
  • OK, thanks for the help, I have it working now.
    What I did was handle the right-click in the datatemplate, and then the following code gets me the actual item that was right-clicked:
         

       private void StackPanel_RightTapped(object sender, RightTappedRoutedEventArgs e)
            {
                var s = (FrameworkElement) sender;
                var d = s.DataContext;
            }


    Visit http://blog.grogansoft.com/ for Windows development fun.

    • Marked as answer by pumpkinszwan Friday, December 25, 2015 11:57 PM
    Friday, December 25, 2015 11:57 PM
  • Wow, thank you! I spent ages trying to work this out or find some information on how to get the item right tapped in a ListView in UWP! Your solution worked perfectly. Thanks!
    Sunday, March 6, 2016 2:19 PM
  • Hi @pumpkinszwan, what did you do to get the data. Sorry I did not get the answer.

    Can you help me? :(

    Wednesday, March 1, 2017 11:28 AM