views:

40

answers:

1

I created a very simple test control that has a Rectangle on a canvas (within other containers, but inconsequential). The Rectangle has event handlers for mouse down, mouse move, and mouse up. If I capture the mouse in the Rectangle's MouseLeftButtonDown event, I do not receive a corresponding MouseLeftButtonUp event.

Some code:

private void rect1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if (_captured = CaptureMouse())
    {
        _offset = new Point(Canvas.GetLeft(rect1), Canvas.GetTop(rect1));
        _origin = e.GetPosition(RootCanvas);
        e.Handled = true;
    }
}


private void rect1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    if (_captured)
    {
        ReleaseMouseCapture();
        _captured = false;
        e.Handled = true;
    }
}

I attached event handlers for the container elements as well, just to make sure one of them wasn't getting the mouse-up event somehow, but none of them were. Is there an expectation of this in Silverlight that I haven't yet learned?

+1  A: 

I think you are little confused about what is actually capturing the mouse events.

Consider when you do this:-

 if (_captured = CaptureMouse())

what object is the CaptureMouse actually being called against?

Answer: The user control for which your code is the code-behind. Had you wanted the rectangle to capture the mouse you would do:-

 if (_captured = rect1.CaptureMouse())
AnthonyWJones
Not so much confused as not paying attention. This is my facepalm moment of the day. Thank you for pointing out what should have been obvious.
oakskc