1

When we have a situation like this:

<Grid x:Name="Grid1" Tapped="Grid1_OnTapped"> 
    <!-- ... -->
    <Grid x:Name="Grid2" Tapped="Grid2_OnTapped">
        <!-- ... -->
           ...  
                     <Grid x:Name="Grid_n" Tapped="Grid_n_OnTapped">
                          <!-- ... -->
                     </Grid>
    </Grid>
</Grid>

When the user taps on Grid_n the Grid_n_OnTapped event method is called, but all event methods of all parent grids is called too. So my question is, is it possible to somehow prevent that, i.e. "Grid1_OnTapped" must be called only when tap's location is in Grid1's area and not on his child's/sub child's area.

arturdev
  • 10,884
  • 2
  • 39
  • 67

2 Answers2

5

Set e.Handled = true (where e is TappedRoutedEventArgs) in Grid_n_OnTapped event handler - it will prevent bubbling event to the element parent.

fex
  • 3,488
  • 5
  • 30
  • 46
1

I would not try to build around that issue and simply check if the original source is correct:

// Deny first
if ((e.OriginalSource as Grid) != this.Grid_n)
    return;
// Normal handler starts here.
Fred
  • 3,324
  • 1
  • 19
  • 29
  • the thing is that the sender in any `Grid_X_OnTapped` is always that `Grid_X`. – arturdev Oct 30 '14 at 11:43
  • Sorry, I just noticed that I made a mistake. I actually posted what I didn't want to post (not really my day today..). You have to check for the original source. I edited the answer. This code works. – Fred Oct 30 '14 at 11:46
  • This answer is also wrong. Because `e.OriginalSource` can be another control than Grid, e.g. `TextBlock` or `Image` – arturdev Oct 30 '14 at 11:58
  • Sure it can be. You would have to add some type checking as well. If you're looking for the deactivation of the event chain, then @fex's answer is probably the way to go here. – Fred Oct 30 '14 at 12:01