以编程方式引发 WPF MouseLeftButtonDownEvent 事件
我试图通过在视觉树上冒泡来引发 MouseLeftButtonDownEvent 使用以下代码。
var args = new MouseButtonEventArgs(Mouse.PrimaryDevice,0,MouseButton.Left);
args.RoutedEvent = UIElement.MouseLeftButtonDownEvent;
args.Source = this;
RaiseEvent(args);
由于某种原因,更高级别的组件没有接收到此冒泡事件。 我是否忽略了某些事情,或者是否无法引发此鼠标事件
I am trying to raise a MouseLeftButtonDownEvent by bubbling it up the Visual tree
with the following code.
var args = new MouseButtonEventArgs(Mouse.PrimaryDevice,0,MouseButton.Left);
args.RoutedEvent = UIElement.MouseLeftButtonDownEvent;
args.Source = this;
RaiseEvent(args);
For some reason the higher level components are not receiving this bubbled event.
Am I overlooking something or is it not possible to raise this Mouse event
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的问题是你正在引发一个不会冒泡的事件。
MouseLeftButtonDownEvent
定义为RoutingStrategy.Direct
,这意味着它仅路由到接收事件的控件。您想改用
Mouse.MouseDownEvent
事件。UIElement
和其他类在内部将其转换为MouseLeftButtonDownEvent
。确保将 e.ChangedButton 设置为MouseButton.Left
:Your problem is that you are raising an event that does not bubble.
MouseLeftButtonDownEvent
is defined asRoutingStrategy.Direct
, which means it is routed to only the control receiving the event.You want to use
Mouse.MouseDownEvent
event instead.UIElement
and other classes internally convert this into aMouseLeftButtonDownEvent
. Make sure you set e.ChangedButton toMouseButton.Left
:我的观点可能是错误的 - 但至少我不久前对
InputManager
进行了相当长的研究。我的简历是:冒泡和隧道是由
InputManager
完成的。然而,调用 uielement.Raise() 只会直接传递事件(无论 Ray Burns 提到的 RoutingStrategy 是什么)。但是(猜测)根据
RoutingStrategy
,InputManager
在CompositionRoot
和VisualTreeHlper.Hittest() 之间的可视化树中上下移动 -编辑视觉并提供隧道和冒泡事件。
有一种方法可以通过 InputManager 引发事件,但它不是官方的,需要反射(我从另一个 Stackoverflow 帖子中得到了它):
I might be wrong in my opinion - but at least I looked some time ago for quite some length into
InputManager
.My resume from that is: The bubbling and tunneling is done by
InputManager
. However callinguielement.Raise()
will only ever deliver the event directly (regardless of theRoutingStrategy
as Ray Burns mentioed).But (guessing) depending on
RoutingStrategy
theInputManager
goes up and down the visual tree betweenCompositionRoot
and theVisualTreeHlper.Hittest()-
ed Visual and delivers tunneling and bublling events.There is a way to raise Events via the InputManager, but it is not official and needs reflection (I have it from another Stackoverflow post):