简体   繁体   English

为整个控件而不是在DrawingData上设置IsMouseOver属性

[英]Set IsMouseOver property for the entire control and not on the DrawingData

I am drawing a triangle using a Path, and the IsMouseOver property is true only if the mouse pointer is over the triangle. 我正在使用路径绘制三角形,并且IsMouseOver属性仅在鼠标指针位于三角形上时才为true。 I would like it to be True also when the pointer is over the background of path (transparent). 当指针位于路径背景(透明)上时,我也希望它也为True。 How can I obtain this result? 如何获得此结果?

<!-- language: lang-xml -->
<Style TargetType="{x:Type Path}">
    <Setter Property="Stretch" Value="Uniform"/>
    <Setter Property="Fill" Value="#FF9C9C9C"/>
    <Setter Property="Cursor" Value="Hand"/>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Fill" Value="#FFBDBDBD"/>
            <Setter Property="Effect">
                <Setter.Value>
                    <DropShadowEffect x:Name="DropShadowEffect" BlurRadius="12" Color="#FF9C9C9C" ShadowDepth="0"/>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>

<Path Margin="6,0,0,0" StrokeThickness="0" Width="35" Height="17" Data="M0,0 L8,5, 0,10 Z"/>

One way is to place a border around the path, and then bind to the border's IsMouseOver property instead of the path's. 一种方法是在路径周围放置边框,然后绑定到边框的IsMouseOver属性(而不是路径)。 For this to work, the border's background has to be explicitly set to "Transparent" (or whatever color you like). 为此,必须将边框的背景显式设置为“透明”(或您喜欢的任何颜色)。

Place a border around the path: 在路径周围放置边框:

<Border x:Name="Border" Background="Transparent" Margin="6,0,0,0" Width="17" Height="17">
    <Path StrokeThickness="0" Data="M0,0 L8,5, 0,10 Z"/>
</Border>

Then bind to the border's IsMouseOver property instead of the path's: 然后绑定到边框的IsMouseOver属性,而不是路径的:

<DataTrigger Binding="{Binding IsMouseOver, ElementName=Border}" Value="True">

Perhaps you have not asked the correct question? 也许您没有提出正确的问题? You showed your code (which works just fine) and said 您显示了代码(效果很好)并说

I would like [the IsMouseOver property to be] True also when the pointer is over the background of path 我希望[ IsMouseOver属性为] True,当指针位于路径的背景上时

The IsMouseOver property is true when you put the mouse over the centre (where the Background is) of the Path . 当您将鼠标放在Path的中心( Background位于其中)上时, IsMouseOver属性 true However, your title says 但是,你的标题说

Set IsMouseOver property for the entire control and not on the DrawingData 为整个控件而不是在DrawingData上设置IsMouseOver属性

which is of course, something very different (and a good reason to read over your question before posting it). 这当然是非常不同的(这是在发布问题之前先仔细阅读您的问题的充分理由)。 If you want to have the Path.Effect change when you put your mouse over the entire control then you simply have to write a Style that accesses that control instead. 如果要在将鼠标置于整个控件上时更改Path.Effect则只需编写一种访问该控件的Style Again, you failed to let us know what type that control might be, so I'll just assume that it is a Window . 同样,您没有让我们知道该控件可能是什么类型,所以我仅假设它是Window Try this: 尝试这个:

<Style TargetType="{x:Type Path}">
    <Setter Property="Stretch" Value="Uniform"/>
    <Setter Property="Fill" Value="#FF9C9C9C"/>
    <Setter Property="Cursor" Value="Hand"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource 
            AncestorType={x:Type Window}}}" Value="True">
            <Setter Property="Fill" Value="#FFBDBDBD"/>
            <Setter Property="Effect">
                <Setter.Value>
                    <DropShadowEffect x:Name="DropShadowEffect" BlurRadius="12" 
                        Color="#FF9C9C9C" ShadowDepth="0"/>
                </Setter.Value>
            </Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>

If that is still not what you're after, then perhaps it would be an idea to be more specific about what you do want. 如果那仍然不是您要追求的目标,那么可能更想明确您想要的目标。


UPDATE >>> 更新>>>

Now that you've explained that the Path is in a Button , I can suggest that you simply add a Style for the Button to change the Cursor : 现在,您已经解释了PathButton ,我建议您只需为Button添加一个Style即可更改Cursor

<Style TargetType="{x:Type Button}">
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Cursor" Value="Hand" />
        </Trigger>
    </Style.Triggers>
</Style>

Since path is a kind of shape so the drawn shape is the region which triggers mouse move all other events are passed to the control below 由于路径是一种形状,因此绘制的形状是触发鼠标移动的区域,所有其他事件都传递给下面的控件

I tried to achieve the same using a different approach 我尝试使用不同的方法来达到相同的目的

style 样式

    <Style TargetType="{x:Type ContentControl}" x:Key="icon">
        <Setter Property="Background" Value="#FF9C9C9C"/>
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="Template" >
            <Setter.Value>
                <ControlTemplate TargetType="ContentControl">
                    <Border Background="Transparent">
                        <Path Stretch="Uniform" Fill="{TemplateBinding Background}" Margin="6,0,0,0" StrokeThickness="0" Data="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="#FFBDBDBD"/>
                <Setter Property="Effect">
                    <Setter.Value>
                        <DropShadowEffect x:Name="DropShadowEffect" BlurRadius="12" Color="#FF9C9C9C" ShadowDepth="0"/>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>

usage 用法

    <ContentControl Style="{StaticResource icon}" Width="35" Height="17" Content="M0,0 L8,5, 0,10 Z"/>

in the style above i add a border with transparent background which effectively creates a transparent region for mouse events thus will be able to capture mouse move over full control 在上面的样式中,我添加了具有透明背景的边框,该边框有效地创建了鼠标事件的透明区域,因此将能够捕获鼠标在完全控制下的移动

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

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