这个问题与another question有关,我也几乎没有问过这个问题。
我有一个包含路径和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属性会像我预期的那样触发路径样式,当鼠标指针位于绘制的路径上时。但是,当鼠标指针位于文本块上时(它的位置正好位于绘制路径的中间),那么路径样式就不会像我预期的那样触发。
为什么它不触发?textblock驻留在画布中,所以从技术上讲,画布上的鼠标指针不是也存在吗?
谢谢您在这方面的任何帮助。
发布于 2013-08-28 02:20:28
原因是,您将触发器的属性设置为Canvas.IsMouseOver,,以便在画布结束时触发触发器。但是当您在路径的样式中设置触发器时,这将限制在路径的区域内。
我知道IsMouseOver是一个属性,但我想以MouseEnter为例。MouseEnter是一个路由事件,所以当鼠标在TextBlock上悬停时,它将以路由事件的形式触发,TextBlock的MouseEnter事件将触发,可能TextBlock的IsMouseOver变为真,而不是路径和画布。因为现在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>https://stackoverflow.com/questions/18473156
复制相似问题