WPF:如何使用透明画布和可单击子项制作覆盖控件 [英] WPF: How to make an overlay control with transparent canvas and clickable child

查看:240
本文介绍了WPF:如何使用透明画布和可单击子项制作覆盖控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个自定义控件,将其用作叠加层.该控件应包含几个子控件,应照常绘制它们并使其可单击.但是控件中的其他所有内容都应该透明且可点击".

I want to make a custom control which will be used as an overlay. The control should contain a couple of child controls which should be drawn and should be clickable as usual. But everything else in the control should be transparent and "clickable-through".

这是我尝试实现此目标的方法...首先,我在要放置叠加层的窗口中使用PreviewMouseDown \ Up \ Move事件.我希望这些事件通过"自定义控件的透明部分,但在不透明的位置停止(例如在我的按钮处).其次,这是我的控件的xaml(未更改root UserControl节点):

Here is how I try to achieve this... First, I'm using PreviewMouseDown\Up\Move events in the window where the overlay is going to be placed. I want these events to "go through" transparent part of my custom control, but stop at not-transparent (for example at my button). Second, here is the xaml for my control (root UserControl node was left untouched):

<Canvas Background="transparent" IsHitTestVisible="true">
     <Button Canvas.Left="384" Canvas.Top="34" Content="Button" Height="23" Name="button1" Width="75" Click="button1_Click" IsHitTestVisible="True" />
     <TextBlock Canvas.Left="27" Canvas.Top="105" Height="36" Name="textBlock1" Text="TextBlock" Width="432" FontSize="24" IsHitTestVisible="False" Foreground="Red" FontWeight="Bold" />
</Canvas>

但是,如果我将Canvas的IsHitTestVisible设置为false,则包括按钮在内的整个控件都将变为"unhittable".如果将其设置为true,则所有隧道事件都将在自定义控件处停止,并且按钮变为不可点击.

However if I set Canvas' IsHitTestVisible to false, the whole control including button becomes "unhittable". If set the it to true my all the tunneling events stop at custom control and button becomes unclickable.

实现这种行为的正确方法是什么? 是否可以在不子类化画布(或其他面板)的情况下做到这一点?

What is the right way to achieve this kind of behavior? Is it possible to do so without subclassing canvas (or any other panel)?

推荐答案

您应将Canvas的背景设置为null(或仅不设置背景,默认为null). Transparent对于鼠标单击是可见".

You should set the background of the Canvas to null (or just no background, null is default). Transparent is "visible" to the mouse clicks.

这篇关于WPF:如何使用透明画布和可单击子项制作覆盖控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆