【问题标题】:IsMouseOver not triggeringIsMouseOver 未触发
【发布时间】:2013-08-27 18:44:13
【问题描述】:

这个问题与another question 有关,我也几乎没有问过这个问题。

我有一个 Canvas,里面有一个 Path 和一个 TextBlock。

<Canvas>
    <Path Name="pathNodeType" StrokeThickness="1">
        <Path.Style>
            <Style>
                <Setter Property="Path.Stroke" Value="Black" />
                <Setter Property="Path.Fill" Value="LightGray" />
                <Style.Triggers>
                    <Trigger Property="Canvas.IsMouseOver" Value="True">
                        <Setter Property="Path.Stroke" Value="Blue" />
                        <Setter Property="Path.Fill" Value="LightBlue" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Path.Style>
        <Path.Data>
            <PathGeometry>
                <PathGeometry.Figures>
                    <PathFigureCollection>
                        <PathFigure IsClosed="True" StartPoint="20,40">
                            <PathFigure.Segments>
                                <PathSegmentCollection>
                                    <ArcSegment Size="10,10" RotationAngle="45" IsLargeArc="True"  SweepDirection="Clockwise"  Point="50,40" />
                                    <LineSegment Point="50,60" />
                                    <LineSegment Point="20,60" />
                                </PathSegmentCollection>
                            </PathFigure.Segments>
                        </PathFigure>
                    </PathFigureCollection>
                </PathGeometry.Figures>
            </PathGeometry>
        </Path.Data>
    </Path>
    <TextBlock HorizontalAlignment="Left" Margin="22,40,0,0" TextWrapping="Wrap" Text="AND" VerticalAlignment="Top" FontWeight="Bold"/>
  </Canvas>

当鼠标指针悬停在绘制的路径上时,画布的 IsMouseOver 属性会触发路径样式,正如我所期望的那样。但是,当鼠标指针位于文本块上(位于绘制路径的中间)时,路径样式不会像我预期的那样触发。

为什么不触发?文本块驻留在画布中,所以从技术上讲,鼠标指针不也是在画布上吗?

在此先感谢您提供任何帮助。

【问题讨论】:

    标签: wpf xaml triggers


    【解决方案1】:

    原因是,您将触发器的属性设置为 Canvas.IsMouseOver,所以当 Canvas 鼠标悬停时它会触发。 但是当您在路径的样式中设置触发器时,这将限制在路径的区域内。

    我知道 IsMouseOver 是一个属性,但我想以 MouseEnter 为例。 MouseEnter 是一个路由事件,所以当鼠标悬停在 TextBlock 上时,它会作为路由事件触发,TextBlock 的 MouseEnter 事件会触发,可能是 TextBlock 的 IsMouseOver 变为 true,而不是 Path 和 Canvas .因为现在TextBlock的ZIndex最高。

    所以,我们需要做的是让 TextBlock 的 IsMouseOver 在悬停时保持不变。

    我们可以设置TextBlock的IsHitTestVisible="False";

    代码可以是这样的: 我已经测试了这段代码,它可以工作!

    <Canvas Background="Transparent">
        <Path Name="PathNodeType" StrokeThickness="1">
            <Path.Style>
                <Style TargetType="Path">
                    <Setter Property="Stroke" Value="Black" />
                    <Setter Property="Fill" Value="LightGray" />
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Stroke" Value="Blue" />
                            <Setter Property="Fill" Value="LightBlue" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Path.Style>
            <Path.Data>
                <PathGeometry>
                    <PathGeometry.Figures>
                        <PathFigureCollection>
                            <PathFigure IsClosed="True" StartPoint="20,40">
                                <PathFigure.Segments>
                                    <PathSegmentCollection>
                                        <ArcSegment IsLargeArc="True"
                                                    Point="50,40"
                                                    RotationAngle="45"
                                                    Size="10,10"
                                                    SweepDirection="Clockwise" />
                                        <LineSegment Point="50,60" />
                                        <LineSegment Point="20,60" />
                                    </PathSegmentCollection>
                                </PathFigure.Segments>
                            </PathFigure>
                        </PathFigureCollection>
                    </PathGeometry.Figures>
                </PathGeometry>
            </Path.Data>
        </Path>
        <TextBlock Margin="22,40,0,0"
                   HorizontalAlignment="Left"
                   VerticalAlignment="Top"
                   FontWeight="Bold"
                   Text="AND" IsHitTestVisible="False"
                   TextWrapping="Wrap" />
    </Canvas>
    

    【讨论】:

    • 感谢您的帮助。我在各个网站上都看到过 IsHitTestVisible,但我还没有研究它的作用。看来我需要稍微阅读一下。 :)
    • IsHitTestVisible 在您想让控件对交互透明时非常有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多