如何在空或透明背景上进行检测? [英] How to hit detect on a null or transparent background?

查看:101
本文介绍了如何在空或透明背景上进行检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,其中包含路径内容的按钮仅检测到路径上的鼠标单击。对于ux,我想在按钮的任何位置注册点击。我将按钮的背景设置为null和透明,以便顶部控件容器指示背景样式。

I'm running into a problem where a button with path content is only detecting mouse clicks on the path. I'd like to, for ux, to have the click registered anywhere in the button. I've set the background of the button to both null and transparent so the top control container dictates the background style.

这里是另一个SO帖子:透明背景上的鼠标事件

Here's another SO post : Mouse event on transparent background

如上所述

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:wpfMyCustomControl">

<ControlTemplate x:Key="IconTemplate" TargetType="{x:Type ContentControl}">
    <Grid>
        <Path Name="ForegroundSymbol" Data="M0,0 L1,0 1,1 0,1 0.5,0.5 z" Fill="{TemplateBinding Foreground}" Stretch="Fill" />
    </Grid>
</ControlTemplate>

<Style x:Key="IconButtonStyle" TargetType="{x:Type RepeatButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type RepeatButton}">
                <Grid>
                    <ContentControl Name="icon" Template="{StaticResource IconTemplate}" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


<ControlTemplate x:Key="MyCustomTemplate" TargetType="{x:Type local:MyCustomControl}">
    <Grid Name="LayoutRoot" Background="Red">
        <RepeatButton Background="{x:Null}" Style="{StaticResource ResourceKey=IconButtonStyle}" />
    </Grid>
</ControlTemplate>

<Style TargetType="{x:Type local:MyCustomControl}">
    <Setter Property="Template" Value="{StaticResource ResourceKey=MyCustomTemplate}" />
</Style>
</ResourceDictionary>

如果我从样式中删除了 x:Key属性,则控件将呈现。我已经能够使用上述xaml控件样式重现该问题,其中点击检测不会在按钮的背景部分触发。

If I remove the "x:Key" attribute from "Style", the control renders. I've been able to reproduce the issue, with the above xaml control style, where the hit detection does not trigger on the "background" portion of the button.

推荐答案

如果您实际使用的背景是透明的,HitTesting将会起作用。您正在应用样式,但未包含样式,因此我不能说,但是如果使用该样式,则要设置模板,而没有在ControlTemplate中使用背景,那么设置背景是没有意义的。通常,模板中根元素的背景与背景(通常是面板或边框)进行TemplateBinding。元素本身不使用背景。另一种选择是重写HitTestCore方法。您可以在TextBlock的实现中查看Reflector / ILSpy,因为它会重写此设置以确保其矩形内的任何点(而不仅仅是文本字符)都是有效的命中点。

HitTesting would work if the Background is Transparent if you're actually using it. You're applying a Style but didn't include the Style so I can't say but if in that Style you're setting the Template and you're not using the Background in the ControlTemplate then setting the Background is meaningless. Typically the Background of the root element in the template does a TemplateBinding to the Background (usually a panel or border). The element itself doesn't use the Background. One other option is to override the HitTestCore method. You can look in Reflector/ILSpy at the TextBlock's implementation as it overrides this to ensure that any point within its rect (and not just the characters of the text) is a valid hit point.

编辑:根据您提供的样式/模板,问题就是我所描述的。您需要使用 ControlTemplate 中的背景,否则不会产生任何影响。因此, IconTemplate 应该看起来像:

Edit: Based on the styles/templates you have provided the problem is what I described. You need to use the Background within the ControlTemplate or it won't have any impact. So the IconTemplate should look like:

<ControlTemplate x:Key="IconTemplate" TargetType="{x:Type ContentControl}">
    <Grid Background="{TemplateBinding Background}">
        <Path Name="ForegroundSymbol" Data="M0,0 L1,0 1,1 0,1 0.5,0.5 z" Fill="{TemplateBinding Foreground}" Stretch="Fill" />
    </Grid>
</ControlTemplate>

这篇关于如何在空或透明背景上进行检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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