我將以下控件集作爲WinForms元素主機(WPF Interop)的Child。當StrokeThickness設置爲低值(即1)時,似乎IsMouseOver屬性沒有一致地設置,並且沒有出現正確的樣式。我確認這個問題不在WinForms中,因爲我也在WPF應用程序中測試了這些功能。這是我造成的問題,因爲我將這些矩形用作自定義圖表控件上的隱藏疊加層。當鼠標進入圖表的某個區域時,它會變亮。使用IsMouseOver屬性時是否存在其他屬性約束(即StrokeThickness)?IsMouseOver上的觸發器需要較大的StrokeThickness
<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>
是的,但後來我失去了在觸發器中設置Fill屬性的能力。作爲替代,我創建了一個輔助觸發器,當IsMouseOver == false時將填充設置爲透明。這似乎工作。如果您對此方法有任何疑慮,請告知我們。 – Bubbles 2011-04-14 21:24:46
你不需要相反的觸發器。如果您在樣式中使用了一個Setter( ),則觸發器會在該值處於活動狀態時覆蓋該值,並在其再次變爲非活動狀態時重置。如果您將填充設置爲矩形上的本地值,則情況並非如此,因爲本地值優先。 –
2011-04-14 22:13:54
感謝您的理解。我不知道觸發器可以覆蓋樣式設置器。 – Bubbles 2011-04-15 15:21:24