我可以仅针对右键事件将 IsHitTestVisible 设置为 false 吗?
显然,这个问题的直接答案是“否”,但是对我来说实现这种效果的最佳方法是什么?为了解释一下,这里有一些背景...
我有一个应用程序,它显示一个图像以及该图像上的几层重叠形状。所有这些都放置在网格单元格内,彼此重叠,图像位于局部 z 顺序的底部。为了提高速度,覆盖层被实现为从 FrameworkElement 派生的类,并且它们创建和管理自己的视觉效果。
下面是一个基本图(尽管已分解为 3D),显示了各层的排列方式以及它们具有/需要的交互:
overlapped_controls.png http://www.afterbang.co.uk/mal/stackoverflow/overlapped_controls.png
正如你所看到的,上面的覆盖层必须允许用户拖出一个新的矩形图像中的任何位置。为此,我给它一个透明的背景,就像一块可以在上面绘图的玻璃板。这样做的明显缺点是它会阻止所有鼠标事件向下传递到较低层。
我真的很想只将鼠标左键的事件捕获在顶层的透明区域中,并让其他事件击中 z 顺序下方的图层。
对于这种事情,我应该遵循 WPF 模式吗?有什么最佳实践或技术吗?
我对 WPF 还很陌生,但想编写一个与 API 协调的解决方案。谢谢。
Obviously, the straightforward answer to the question is "No", but what's the best way for me to achieve that kind of effect? To explain, here's a bit of background...
I have an app that displays an Image plus a couple of layers of overlaid shapes on that image. All of these are placed within a Grid cell, overlapping each other, with the image at the bottom of the local z-order. For speed, the overlays are implemented as classes derived from FrameworkElement and they create and manage their own visuals.
Here's a basic diagram (albeit exploded into 3D) of how the layers are arranged and what interaction they have/need:
overlapped_controls.png http://www.afterbang.co.uk/mal/stackoverflow/overlapped_controls.png
As you can see, the upper overlay must allow the user to drag out a new rectangle anywhere in the image. To that end, I've given it a transparent background, acting like a plate of glass on which you can draw. The obvious drawback of this is that it stops all mouse events passing through down to the lower layers.
I'd really like to only trap the left mouse button's events in that top layer's transparent area and let other events hit layers further down the z-order.
Is there a WPF-minded pattern I should be following for this kind of thing? Any kind of best practice or technique?
I'm still fairly fresh to WPF, but want to write a solution that's in tune with the API. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将在需要右键单击的元素上使用
PreviewMouseDown
事件,并在需要左键单击的元素上使用LeftMouseDown
事件。PreviewMouseDown
事件仍然会触发,但您可以编写处理程序来忽略它的左键单击。这实际上是我用来在图像顶部显示形状的模式,以便用户可以圈出图像的一部分以突出显示它。我在图像上有
PreviewMouseDown
,并且它始终正常工作,然后我在任何形状上有一个LeftMouseDown
处理程序,以便可以移动、删除它们等。I would use the
PreviewMouseDown
event on the elements that need to be right-clicked, and useLeftMouseDown
on the elements that need to be left-clicked. ThePreviewMouseDown
event will still fire, but you can write handlers to ignore left clicks for it.This is actually a pattern I'm using to display shapes on top of images, so that a user can circle a part of the image to highlight it. I have the
PreviewMouseDown
on the image, and it functions normally all the time, then I have aLeftMouseDown
handler on any shapes so that they can be moved, deleted, etc.