locked
How to handle Tap event of An Image within a ListBox in wp8.

    Question

  • Hi,

    In my Application I want to handle Tap Event of an image within a ListBox

    but the problem is that when I tap on the Image then first the ListBox selection changed event is fired then the Image Tap event is fired. I have to do some other task in ListBox Selection Changed and some other task in image Tap.

    Please tell me how to do That. 


    • Edited by ved3397 Friday, January 30, 2015 9:11 AM
    Friday, January 30, 2015 9:10 AM

Answers

  • The ListBox.SelectionChanged event will always fire before the Tapped event, but once the item is selected, it won't fire again until the selected item changes.  That means you can remove the tapped event from the image until the item is selected.  Once it is selected, enable the tapped event so you can do stuff again when the tapped event occurs.

    There are some different ways to make this happen, (like unsubscribing the event handler) but here's a simpler one:

    bool TappedEnabled = false;
    private void Image_Tapped(object sender, TappedRoutedEventArgs e)
    {        {
      if (TappedEnabled)
      {
        // Do stuff
      }
      else TappedEnabled = true;
    }
    
    private void MyListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      TappedEnabled = false;
      // Do stuff;
    }


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, January 30, 2015 4:17 PM
    Moderator

All replies

  • The ListBox.SelectionChanged event will always fire before the Tapped event, but once the item is selected, it won't fire again until the selected item changes.  That means you can remove the tapped event from the image until the item is selected.  Once it is selected, enable the tapped event so you can do stuff again when the tapped event occurs.

    There are some different ways to make this happen, (like unsubscribing the event handler) but here's a simpler one:

    bool TappedEnabled = false;
    private void Image_Tapped(object sender, TappedRoutedEventArgs e)
    {        {
      if (TappedEnabled)
      {
        // Do stuff
      }
      else TappedEnabled = true;
    }
    
    private void MyListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      TappedEnabled = false;
      // Do stuff;
    }


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, January 30, 2015 4:17 PM
    Moderator
  • Thanks  Matt Small
    Tuesday, February 17, 2015 12:28 PM