我有两个相交的矩形。当鼠标悬停在它们上面时,我想改变两者的不透明度。当鼠标放在其中一个上时,它就会工作。但是,当鼠标位于矩形的相交区域时,只有上面的矩形改变其不透明度。你能告诉我在这种情况下我怎样才能让这两个矩形改变不透明度吗?
<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>
实际上与HiTech Magic描述的方法相似:
<Window.Resources>
<Style x:Key="RectangleHighlighter" TargetType="{x:Type Rectangle}">
<Setter Property="Opacity" Value="0.25" />
<Style.Triggers>
<Trigger Property="Tag" Value="MouseOver">
<Setter Property="Opacity" Value="1" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid x:Name="LayoutRoot" Background="Transparent" MouseMove="LayoutRoot_MouseMove">
<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>
及其背后的代码:
private List<DependencyObject> hitResultsList = new List<DependencyObject>();
private void LayoutRoot_MouseMove(object sender, MouseEventArgs e)
{
// Retrieve the coordinate of the mouse position.
Point pt = e.GetPosition((UIElement)sender);
// Clear the contents of the list used for hit test results.
hitResultsList.Clear();
// Set up a callback to receive the hit test result enumeration.
VisualTreeHelper.HitTest(LayoutRoot, null,
new HitTestResultCallback(MyHitTestResult),
new PointHitTestParameters(pt));
// Unset all children
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(LayoutRoot); ++i)
{
var element = VisualTreeHelper.GetChild(LayoutRoot, i) as FrameworkElement;
if (element != null)
{
element.Tag = null;
}
}
// Perform actions on the hit test results list.
foreach (var dependencyObject in hitResultsList)
{
var element = dependencyObject as FrameworkElement;
if (element != null)
{
element.Tag = "MouseOver";
}
}
}
// Return the result of the hit test to the callback.
public HitTestResultBehavior MyHitTestResult(HitTestResult result)
{
// Add the hit test result to the list that will be processed after the enumeration.
hitResultsList.Add(result.VisualHit);
// Set the behavior to return visuals at all z-order levels.
return HitTestResultBehavior.Continue;
}
当然,在这种情况下,最好添加一些特殊的附加属性和行为
您需要将悬停处理程序添加到父网格中,使两个矩形都具有IsHitTestVisible=False并使用VisualTreeHelper。FindElementsInHostCoordinates来确定实际在鼠标下面的对象(1个或更多)。
如果你必须让它基于样式,马里奥·维尔纳里的建议将工作,但悬停将触发在网格的任何地方,而不仅仅是在矩形。
这对我来说是一个非常有用的附加行为的想法。这种行为将实现上面列出的代码,但会触发子对象上的悬停事件,所以你仍然可以使用样式来实现……我得试试
尝试将样式应用到Grid元素