2
votes

I've created the following control:

<UserControl x:Class="FooBar.AnnotationControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="400" Width="500" >
    <ScrollViewer Height="400" Width="500">
        <Canvas Height="400" Width="500" Name="ctlCanvas" MouseLeftButtonDown="MouseLeftButtonDownHandler" >
            <Canvas.RenderTransform>
                <ScaleTransform x:Name="ZoomTransform" />
            </Canvas.RenderTransform>
        </Canvas>
    </ScrollViewer>
</UserControl>

namespace FooBar
{

    public partial class AnnotationControl : UserControl
    {
        public AnnotationControl()
        {
            InitializeComponent();

        }

        private void MouseLeftButtonDownHandler( object sender, MouseButtonEventArgs args)
        {
           //Do Something
        }

    }

}

when I click the canvas, I don't hit breakpoints in the MouseLeftButtonDownHandler. I even attach this handler to the ScrollViewer and get the same result. Any idea what's going on here?

1
Can you try adding the event handler manually in the constructor, using the AddHandler(...) method. That way, you can specify to handle events that have already been handled (in case something is swallowing the event). If you still get no luck, then something may be preventing the event from firing.Samuel Slade

1 Answers

1
votes

The default background for a Canvas is Transparent, which allows hit tests to pass through it. To make your Canvas register for HitTests, give it a Background Color.

<Canvas Background="White" ... />