【问题标题】:How to hit detect on a null or transparent background?如何在空或透明背景上点击检测?
【发布时间】:2013-03-02 00:48:59
【问题描述】:

我遇到了一个问题,即带有路径内容的按钮仅检测鼠标在路径上的点击。对于 ux,我想在按钮的任何位置注册点击。我已将按钮的背景设置为 null 和透明,因此顶部控件容器决定了背景样式。

这是另一个 SO 帖子:Mouse event on transparent background

如前所述,到目前为止,我已经尝试过透明和 null。

<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 控件样式重现该问题,其中点击检测不会在按钮的“背景”部分触发。

【问题讨论】:

  • 如果您提供一些代码供我们查看,将会非常有帮助。
  • iconstyle 怎么样?任何问题很可能与控制模板的VisualTree 有关。
  • 我无法使用按钮、您的 &lt;Path&gt; 和按钮的 {x:Null} 背景来重现此内容。这证明是您的Style 造成了这种情况。开始解构样式直到问题消失,或者反过来开始:从空白样式开始并添加东西直到问题出现。无论哪种方式,您都可以准确找到导致问题的原因,进而帮助您解决问题。

标签: wpf custom-controls


【解决方案1】:

您应该使用几乎的背景,而不是使用透明背景 透明。这样,Click-事件仍将被触发,但按钮仍将显示为透明。
类似的东西:

<RepeatButton Background="#01FFFFFF" Style="{StaticResource ResourceKey=IconButtonStyle}" />

颜色定义的前两位数字定义了TransparencyAlpha 值。

【讨论】:

  • 看起来更像 Opacity 而不是 Transparency 然后
【解决方案2】:

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

编辑:根据您提供的样式/模板,问题就是我所描述的。您需要在 ControlTemplate 中使用 Background,否则不会产生任何影响。所以 IconTemplate 应该是这样的:

<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>

【讨论】:

  • 你们让我找到了其他显然是问题的东西。在我的“
  • 不确定你的意思。在您提供的 sn-p 中,您将按钮的样式设置为带有 iconStyle 键的样式。你能发布那个风格吗?如果它有一个用于 Template 属性的 Setter,请确保包含该 ControlTemplate。
  • 好的,我看到你发布了样式。问题正如我所提到的 - 你实际上并没有使用背景,所以设置它没有影响。因此,如果您想解决这个问题,您可以将 Background="{TemplateBinding Background}" 添加到 IconTemplate 的网格中。
  • 这就是答案。虽然我确实必须将“背景”绑定添加到样式“IconButtonStyle”的“控制模板”中。它没有像上面发布的那样做任何事情。我无法解释。谢谢!
  • 您仍然需要将按钮的背景设置为画笔。 Null 不会进行测试,但透明(或任何其他颜色)画笔会。
【解决方案3】:

如果您不想设置颜色而不是设置透明,则需要为命中检测提供背景颜色。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    相关资源
    最近更新 更多