c# - 为什么没有 MouseMoveEvent——或者,如何为鼠标移动事件使用 AddHandler

标签 c# .net silverlight mouseevent routed-events

UIElement类定义静态 RoutedEvent 成员 MouseLeftButtonDownEventMouseLeftButtonUpEvent - 但没有 MouseMoveEvent。据我所知,框架层次结构中的任何类都没有。有常规事件定义:

public event MouseEventHandler MouseMove;

所以你可以这样写:

void AttachHandler(UIElement element)
{
    element.MouseMove += OnMouseMove;
}

但您不能使用其他形式,它允许您订阅甚至已处理的事件:

void AttachHandler(UIElement element)
{
    element.AddHandler(UIElement.MouseMoveEvent, new MouseEventHandler(OnMouseMove), true);
}

所以我的问题是双重的:

  1. 为什么没有在任何地方定义MouseMoveEvent
  2. 是否有一种变通方法可以让您在处理 MouseMove 事件时收到通知?

编辑

我看到了 the MSDN docs acknowledge this as a limitation :

A limitation of this technique is that the AddHandler API takes a parameter of type RoutedEvent that identifies the routed event in question. Not all Silverlight routed events provide a RoutedEvent identifier, and this consideration thus affects which routed events can still be handled in the Handled case.

编辑 #2

根据@HansPassant,一般答案是“MouseMove”事件不能标记为“已处理”,因此它们总是冒泡。对于 TextBox 也是如此,除了一个明显的边缘情况:当您单击 TextBox 的文本区域时,从而激活拖动选择的东西,“MouseMove”事件不再被触发。我不知道为什么会这样。


注意 -- 对于任何好奇的人 -- 我正在尝试编写一个允许用户拖/放文本框的行为。 TextBox 控件默认拦截鼠标事件,以允许选择文本。

最佳答案

它在 MSDN article 中明确提及:

MouseMove cannot be used with AddHandler because there is no Handled in its event data

这样就回答了你的问题:

Why is there no MouseMoveEvent defined anywhere?

因为不需要。

Is there a workaround that allows you to get a notification for MouseMove events even when they are handled?

你不需要一个,它们无法处理,因此总是起泡。 Window 的 MouseMove 事件处理程序将看到它们。

关于c# - 为什么没有 MouseMoveEvent——或者,如何为鼠标移动事件使用 AddHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23791210/

相关文章:

java - 您推荐使用什么平台来开发 SAAS 应用程序?

c# - 谁在 Dictionary<>.First() 上?

c# - 从 JSON 确定对象类型

c# - 打开前置摄像头

C# Entity Framework (.Net Core)验证(服务器端)如何?

css - 使 Silverlight 应用程序在不同的浏览器缩放级别下工作

c# - 是否可以通过单击按钮事件导航到枢轴控制页面

.net - EF4 + MVVM - 在 ViewModel 中公开实体?

c# - Win7的Virtual Machines文件夹如何获取?

c# - 补高阶函数