简体   繁体   English

IsMouseOver 触发器仅在光标位于按钮的“底部”部分时返回 true

[英]IsMouseOver trigger only returns true when the cursor is over the 'bottom' part of the button

I am fairly new to WPF, and I am trying to make a custom button where it changes to another specified colour when you hover over it.我对 WPF 相当陌生,我正在尝试制作一个自定义按钮,当您将鼠标悬停在它上面时,它会更改为另一种指定的颜色。 I have done this with partial success;我已经部分成功地做到了这一点; the only problem is that only the bottom part of the button actually triggers the colour change.唯一的问题是只有按钮的底部才会真正触发颜色变化。

Red highlighted area is the approximate hitbox.红色突出显示的区域是近似的命中框。 (not the long red strip, that's just decoration) (不是长红条,那只是装饰)

在此处输入图片说明

<!-- Button Markup-->
<Button Margin="4,0,4,0" >
    <Image Source="{StaticResource closeImg}"></Image>
</Button>

<!-- Button style -->

<Style TargetType="{x:Type Button}">
    <Setter Property="Background" Value="#FF2B2B2B"/>
    <Setter Property="HorizontalAlignment" Value="Right"/>
    <Setter Property="Width" Value="28px"/>
    <Setter Property="Height" Value="28px"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{TemplateBinding Background}">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Red"/>
        </Trigger>
    </Style.Triggers>
</Style>

I've found that this was a slight quirk with the debug function with WPF and the WindowChrome class.我发现这是一个带有 WPF 和 WindowChrome 类的调试功能的小怪癖。 This is fixed by adding the following to the style:这是通过在样式中添加以下内容来解决的:

<Setter Property="WindowChrome.IsHitTestVisibleInChrome" Value="True"></Setter>

your solution seems to be good to me.你的解决方案对我来说似乎很好。 I've tried the same thing on my vs and everything seems to be just fine.我在我的 vs 上尝试过同样的事情,一切似乎都很好。 Perhaps there's something else connected with container that holds these buttons, or perhpas something with the image itself.也许还有其他东西与保存这些按钮的容器有关,或者可能与图像本身有关。 Can you try to set the name for the border inside of a controlTemplate and set the TargetName inside of a trigger?您可以尝试在 controlTemplate 内设置边框的名称并在触发器内设置 TargetName 吗? Wonder if that could help想知道这是否有帮助

<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="#FF2B2B2B"/>
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="Width" Value="28px"/>
<Setter Property="Height" Value="28px"/>
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type Button}">
            <Border x:Name="Bd" Background="{TemplateBinding Background}">
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter TargetName="Bd" Property="Background" Value="Red"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter.Value>
</Setter>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM