I have the following control set as the Child of a WinForms element host (WPF Interop). It seems when the the StrokeThickness is set to low values (i.e. 1), the IsMouseOver property is not consistently set and the correct styling does not appear. I confirmed the issue is not with WinForms, since I also tested the functionality in a WPF app. This is causing me issues because I’m using these rectangles as hidden overlays on a custom charting control. When the mouse enters a certain region of the chart, it becomes highlighted. Are there additional property constraints (i.e. StrokeThickness) when using the IsMouseOver property?
<UserControl.Resources>
<Style x:Key="myStyle" TargetType="{x:Type Rectangle}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Fill" Value="Gold"/>
</Trigger>
</Style.Triggers>
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="Auto"/>
<Setter Property="Stroke" Value="Black"/>
<Setter Property="StrokeThickness" Value="10"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
</Style>
</UserControl.Resources>
<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Height="Auto" Width="Auto">
<StackPanel Orientation="Horizontal">
<Rectangle Style="{StaticResource myStyle}"/>
<Rectangle Style="{StaticResource myStyle}"/>
<Rectangle Style="{StaticResource myStyle}"/>
<Rectangle Style="{StaticResource myStyle}"/>
<Rectangle Style="{StaticResource myStyle}"/>
</StackPanel>
</Grid>
That’s because you’ve not set a background (
Fill) for the rectangle. As such, the mouse is only considered over the rectangle when it’s over the stroke. Set theFillto something other thannull, even if it’sTransparent.