我创建了一个非常简单的测试控件,它有一个 Rectangle
在 Canvas 上(在其他容器内,但无关紧要)。 Rectangle
具有用于鼠标按下、鼠标移动和鼠标抬起的事件处理程序。如果我在 Rectangle 的 MouseLeftButtonDown 事件中捕获鼠标,则不会收到相应的 MouseLeftButtonUp 事件。
一些代码:
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;
}
}
我也为容器元素附加了事件处理程序,只是为了确保其中一个没有以某种方式获得鼠标向上事件,但没有一个。在 Silverlight 中是否有我尚未了解的预期?
最佳答案
我认为您对实际捕获鼠标事件的内容并不感到困惑。
考虑何时执行此操作:-
if (_captured = CaptureMouse())
CaptureMouse
是什么物体居然被骂?答案:您的代码是其代码隐藏的用户控件。如果您想让矩形捕获鼠标,您会这样做:-
if (_captured = rect1.CaptureMouse())
关于silverlight - 鼠标捕获后 Silverlight 鼠标向上事件丢失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3265409/