2

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. Commented Dec 16, 2011 at 15:02

1 Answer 1

1

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" ... />
Sign up to request clarification or add additional context in comments.

1 Comment

Interestingly enough, I was setting the background in the codebehind, I pulled it out with all the other code. This only works when you set it in the xaml. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.