Loss of Silverlight triggers events after mouse capture?

I created a very simple test control that has a Rectangle on the canvas (in other containers, but inconsequential). Rectangle has event handlers for the mouse, mouse, and mouse. If I grabbed the mouse in the Rectangle MouseLeftButtonDown event, I do not get the corresponding MouseLeftButtonUp event.

Some codes:

 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 hooked up event handlers for the elements of the container, just to make sure that one of them did not receive the mouse-up event in some way, but none of them were. Is there any expectation of this in Silverlight that I have not yet recognized?

+4
source share
2 answers

I think you're a little confused about what really captures mouse events.

Consider when you do this: -

  if (_captured = CaptureMouse()) 

which object is CaptureMouse actually called?

Answer. A user control for which your code is code. If you would like the rectangle to capture the mouse, you would do: -

  if (_captured = rect1.CaptureMouse()) 
+2
source

CaptureMouse (); from the mouseDown event, and then try.

+1
source

Source: https://habr.com/ru/post/1315924/


All Articles