locked
UserControl not handling Tapped Event

    Question

  • I have a UserControl that I want to handle a Tapped event. The UserControl has several other controls on top of it (Polygon, Ellipse, Rectangle, and TextBlock controls in my case). These controls will obviously be the first to receive the event, which I did not add a handler to them for, because I want to handle it with the Tapped handler for my UserControl. I have not set the IsHitTestVisible property to False for any of these controls, so I figured the event would work it's way up to the Tapped handler for my UserControl. If I tap any of these controls, nothing seems to happen, but if I tap my UserControl directly (the area not covered by the other controls), the handler is triggered like I wanted. Did I do something that is preventing the event from getting to my UserControl? Do any of these controls (Polygon, Ellipse, Rectangle, and TextBlock) not pass the event on? What can I do to make my UserControl handle the Tapped control instead of the controls on top of it? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Thursday, April 3, 2014 2:14 AM

Answers

  • The behavior you describe is expected. The tap will route up (like in my case) but not sideways (as in yours). An element on top of another element will block the touch, unless the top element is transparent to hit tests. If you set hittestvisible to false the taps should pass through.
    Thursday, April 3, 2014 3:33 AM
    Owner

All replies

  • That should work in theory, and it works for me in a quick test. Can you provide a minimal sample that shows what exactly you're doing? Tapping anywhere in the Canvas, including on the Rectangles, Ellipses, and TextBlock, triggers the Tapped event.

       <Canvas Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Tapped="Canvas_Tapped">
            <Rectangle Fill="Red" Height="100" Width="100" Canvas.Left="1016" Canvas.Top="584"/>
            <Rectangle Fill="Orange"  Height="100" Width="100" Canvas.Left="860" Canvas.Top="92"/>
            <Rectangle Fill="Yellow"  Height="100" Width="100" Canvas.Left="388" Canvas.Top="220"/>
            <Ellipse Fill="Green"  Height="100" Width="100" Canvas.Left="928" Canvas.Top="320"/>
            <Ellipse Fill="Blue"  Height="100" Width="100" Canvas.Left="512" Canvas.Top="504"/>
            <Ellipse Fill="Violet"  Height="100" Width="100" Canvas.Left="68" Canvas.Top="320"/>
            <TextBlock Text="Lorem ipsum" Canvas.Left="236" Canvas.Top="92" FontSize="48" />
        </Canvas>
            int taps = 0;
            private void Canvas_Tapped(object sender, TappedRoutedEventArgs e)
            {
                Debug.WriteLine("Tappity {0}",taps++);
            }

    It also works if I moved the Tapped handler to a UserControl containing this Canvas.

    --Rob


    Thursday, April 3, 2014 2:28 AM
    Owner
  • Your layout is slightly different from what I am doing. All of my controls (both the UserControl and the others) are children of the same Grid, and they are visually on top of each other by setting their Grid.Column and Grid.Row to the same values. So the parent (the Grid) is not what I want to handle the event, the UserControl and the controls on top of it are actually siblings, they are all immediate children of the Grid. Any ideas? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Thursday, April 3, 2014 2:42 AM
  • The behavior you describe is expected. The tap will route up (like in my case) but not sideways (as in yours). An element on top of another element will block the touch, unless the top element is transparent to hit tests. If you set hittestvisible to false the taps should pass through.
    Thursday, April 3, 2014 3:33 AM
    Owner
  • Thank you, once I added IsHitTestVisible="False" to all the elements covering my UserControl it worked like I wanted. I used to think that if IsHitTestVisible="False" was applied to anything covering another element it would stop the event from continuing to the hidden controls, I guess I was a little confused about the difference between what happens for parent/child coverings vs sibling coverings. Thank you for your help.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Thursday, April 3, 2014 6:10 PM