将鼠标悬停在标签更改矩形背景渐变上 [英] Hover over label change rectangle background gradient

查看:84
本文介绍了将鼠标悬停在标签更改矩形背景渐变上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩形,上面有几个标签和图像,而且当用户将鼠标悬停在矩形上时,背景会变为渐变:

I have a rectange with several labels and images over it, and I have it so that when the user hovers their mouse over the rectangle the background changes to a gradient:

<Rectangle Height="88" HorizontalAlignment="Left" Margin="54,28,0,0" Name="rectangle2" VerticalAlignment="Top"
        Width="327" Cursor="Hand">
    <Rectangle.Style>
        <Style TargetType="{x:Type Rectangle}">
            <Setter Property="Fill" Value="Transparent" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Fill">
                        <Setter.Value>
                            <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                <GradientStop Color="White" Offset="0.0" />
                                <GradientStop Color="#eee" Offset="1" />
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Rectangle.Style>
</Rectangle>

但是,当我将鼠标悬停在矩形上方的标签之一上时,背景渐变不会显示

However, when I hover over one of the labels that is over the rectangle the background gradient does not show.

我要制作它,以便将鼠标悬停在标签和矩形上时显示渐变。

I want to make it so that the gradient shows when hovering over the labels as well as the rectangle.

这可能吗?

推荐答案

如果通过 over表示覆盖,而不是在上方,则可以将内容包装在Grid中(对于以上情况,我想您也可以这样做,但是您应该定义行和列)并使用 DataTrigger 会触发此操作,如果鼠标位于包装网格上方,不仅是矩形本身,例如:

If by "over" you mean overlayed and not above you can wrap the contents in a Grid (for above you could do this as well i guess, but you should define rows & columns) and use a DataTrigger which triggers if the mouse is over the wrapping grid and not only the rectangle itself, e.g.:

<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
    <Rectangle Width="100" Height="100" StrokeThickness="1" Stroke="Black">
        <Rectangle.Style>
            <Style TargetType="{x:Type Rectangle}">
                <Setter Property="Fill" Value="Transparent" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource AncestorType=Grid}}" Value="True">
                        <Setter Property="Fill">
                            <Setter.Value>
                                <!-- Brush here -->
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Rectangle.Style>
    </Rectangle>
    <Label Name="label" Content="This is a Label" />
</Grid>

或者,如果标签被覆盖,则可以通过设置<$ c $来使鼠标事件通过标签c> IsHitTestVisible 到 false

Alternatively if the label is overlayed you can make mouse events pass through the Label by setting IsHitTestVisible to false.

这篇关于将鼠标悬停在标签更改矩形背景渐变上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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