传递给 OnPreviewMouseLeftButtonDown 重写的 args 对象似乎描述了当前(实时)鼠标按钮状态,而不是事件发生时存在的状态快照。
这是正确的行为吗?事件参数不应该反射(reflect)事件发生时的事件数据(快照)并且不应该自动更新以反射(reflect)实时数据吗?
谢谢你,
本
代码示例
protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
{
// before MessageBox display, e.ButtonState = Pressed
MessageBox.Show("OnPreviewMouseLeftButtonDown");
// now, e.ButtonState = Released
base.OnPreviewMouseLeftButtonDown(e);
}
最佳答案
我发现了为什么 e.ButtonState 的值在事件处理程序中发生变化——每次访问该属性时,都会调用底层 MouseDevice
它返回当前(实时)按钮状态与事件发生时存在的状态。 (感谢 Microsoft 的 Bob Bao 提供 pointing this out。)
我已经在 http://bengribaudo.com/blog/2010/07/26/38/event-arguments-static-snapshots 上写了关于此的博客.
关于c# - 事件参数——它们应该反射(reflect)事件发生时的状态快照还是实时数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3214392/