简体   繁体   English

在 WPF 中拖放重叠的 UI 对象时如何停止事件传播

[英]How to stop event propagation when dragging and dropping overlapping UI objects in WPF

I have a use case for dragging an item onto an area which could have multiple UI layers.我有一个用例将项目拖到可能有多个 UI 层的区域上。

Use Case 1: Drag and drop a node from a treeview onto a workspace area(drop target).用例 1:将节点从 treeview 拖放到工作区区域(放置目标)。 A chart is created in that workspace area.在该工作区中创建一个图表。

Use Case 2: Drag and drop a node from a treeview onto a chart, that sits above the original workspace area.用例 2:将节点从 treeview 拖放到位于原始工作区上方的图表上。 Extra data is added to the chart.额外的数据被添加到图表中。

The problem with my implementation is that when I conduct Use Case 2, the drop event for the workspace area (Grid) AND chart are called.我的实现的问题是,当我执行用例 2 时,会调用工作区(网格)和图表的放置事件。 Not just the one that is visible.不仅仅是可见的。

How are others implementing this scenario?其他人如何实施此方案?

To stop routed event propagation set e.Handled = true in drop event handler.要停止路由事件传播,请在 drop 事件处理程序中设置 e.Handled = true。

I would not call your case as "multi-layer".我不会将您的案例称为“多层”。 Instead, it is a normal hierarchy of visual objects.相反,它是视觉对象的正常层次结构。 For "multi-layer" I mean, for instance, a drop on several sibling canvas.对于“多层”,我的意思是,例如,几个兄弟 canvas 的下降。

Anyway, assuming that there is an unique point where the drop-action in managed, I would listen to the drop event (which is bubbling).无论如何,假设在托管的放置动作中有一个独特的点,我会收听放置事件(冒泡)。 In the event handler, there is the " OriginalSource " parameter that indicates the very first visual object involved.在事件处理程序中,有一个“ OriginalSource ”参数,指示所涉及的第一个可视 object。 At this point, you may scan the visual tree upward ( VisualTreeHelper ), searching for a "chart" element: if you find it, the drop was on a chart, otherwise was on the empty workspace area.此时,您可以向上扫描可视化树( VisualTreeHelper ),搜索“图表”元素:如果找到,则表示该下拉菜单位于图表上,否则位于空白工作区。

Hope it helps.希望能帮助到你。

Cheers干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM