我可以仅针对右键事件将 IsHitTestVisible 设置为 false 吗?

发布于 2024-08-18 03:01:08 字数 719 浏览 10 评论 0原文

显然,这个问题的直接答案是“否”,但是对我来说实现这种效果的最佳方法是什么?为了解释一下,这里有一些背景...

我有一个应用程序,它显示一个图像以及该图像上的几层重叠形状。所有这些都放置在网格单元格内,彼此重叠,图像位于局部 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尹雨沫 2024-08-25 03:01:08

我将在需要右键单击的元素上使用 PreviewMouseDown 事件,并在需要左键单击的元素上使用 LeftMouseDown 事件。 PreviewMouseDown 事件仍然会触发,但您可以编写处理程序来忽略它的左键单击。

这实际上是我用来在图像顶部显示形状的模式,以便用户可以圈出图像的一部分以突出显示它。我在图像上有 PreviewMouseDown ,并且它始终正常工作,然后我在任何形状上有一个 LeftMouseDown 处理程序,以便可以移动、删除它们等。

I would use the PreviewMouseDown event on the elements that need to be right-clicked, and use LeftMouseDown on the elements that need to be left-clicked. The PreviewMouseDown 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 a LeftMouseDown handler on any shapes so that they can be moved, deleted, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文