获取 RoutedEvent Source 作为处理程序的孙子
我正在 WPF 应用程序中处理用户输入,使用 RoatedEvents
,例如 MouseDown
、MouseMove
等。我已经很好地掌握了来自 MSDN 的路由事件概述,但我遇到了冒泡事件的问题。
当 MouseDown
事件冒泡到处理程序时,MouseButtonEventArgs.Source
为我提供事件来源的当前元素的子元素。有什么方法可以获取 that 元素的子元素吗?我正在考虑 MouseButtonEventArgs.Source.Source
的方式,
在这种情况下,我想将输入处理尽可能地放在应用程序中,但仍然获取有关初始元素的信息事件来自。
下面是一个例子:这里点击了名为 Grandchild 的 Grid,我想在 Parent_MouseDown() 中处理该事件。但是,在这种情况下,我获得的有关事件源的所有信息都是有关名为 Child 的网格的信息。有没有办法从父母那里获取有关孙子的信息?
<Grid x:Name="Parent" MouseDown="Parent_MouseDown">
<Grid x:Name="Child" MouseDown="Child_MouseDown">
<Grid x:Name="Grandchild" MouseDown="Grandchild_MouseDown">
</Grid>
</Grid>
</Grid>
请注意,此示例经过简化,因为在我的应用程序中,这些元素属于不同的类,并且驻留在不同的文件中。提前致谢!
I'm handling user input in a WPF application, working with RoutedEvents
such as MouseDown
, MouseMove
etc. I've got a good grip of tunneling and bubbling from MSDN's Routed Events Overview, but I've run into a problem with bubbling events.
As a MouseDown
event bubbles up to a handler, MouseButtonEventArgs.Source
gives me the child of the current element from which the event came. Is there any way to get a hold of the child of that element? I'm thinking something in the way of MouseButtonEventArgs.Source.Source
In this case I'd like to put my input handling as far up in my application as possible, but still get information about the initial element the event came from.
An example below: here the Grid
named Grandchild is clicked, and I would like to handle the event in Parent_MouseDown()
. However, in that case all the info I get about the source of the event is about the grid
named Child. Is there a way to get information about Grandchild from Parent?
<Grid x:Name="Parent" MouseDown="Parent_MouseDown">
<Grid x:Name="Child" MouseDown="Child_MouseDown">
<Grid x:Name="Grandchild" MouseDown="Grandchild_MouseDown">
</Grid>
</Grid>
</Grid>
Note that this example is simplified, as in my application these elements are of different classes, and reside in different files. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
MouseButtonEventArgs.OriginalSource
它始终指向事件冒泡的原始源。Try
MouseButtonEventArgs.OriginalSource
It always point to the original source from where the event bubbled.