我有兩個相交的矩形。當鼠標懸停在它們上方時,我希望它們的不透明度發生改變。它適用於鼠標懸停在其中一個上面的情況。但是當鼠標位於矩形的相交區域時,只有上面的矩形改變其不透明度。你能否讓我知道在這種情況下如何讓兩個矩形改變不透明度?MouseEvent不起泡
<Window x:Class="WpfTestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfTestApp="clr-namespace:WpfTestApp" Title="MainWindow" Height="350" Width="525" >
<Window.Resources>
<Style x:Key="RectangleHighlighter" TargetType="{x:Type Rectangle}">
<Setter Property="Opacity" Value="0.25" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Opacity" Value="1" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<Rectangle Stroke="Black" Width="100" Fill="Green" Height="1000" Margin="0,15,0,0" Style="{StaticResource RectangleHighlighter}"/>
<Rectangle Stroke="Black" Width="1000" Fill="Green" Height="100" Margin="0,15,0,0" Style="{StaticResource RectangleHighlighter}"/>
</Grid>
</Window>
感謝Kreol的回覆。正如我在HiTech評論中所提到的那樣,我更傾向於使用僅限xaml的解決方案。如果你可以建議一個xaml唯一的解決方案,它將是偉大的,否則我會嘗試這個。 –
不幸的是,我猜這個任務沒有「xaml-only」解決方案。所以我建議你 - 只是將所有這些代碼分離爲一個容器控件的特殊附加屬性(行爲),當然還要添加一些附加屬性,而不是在我的示例中使用的標記。通過這種方式,您將能夠使用這些來自XAML的附加屬性而無需代碼隱藏。 – Kreol