gpt4 book ai didi

wpf - MouseEvent 不冒泡

转载 作者:行者123 更新时间:2023-12-01 07:52:04 24 4
gpt4 key购买 nike

我有两个相交的矩形。当鼠标悬停在它们上方时,我希望两者的不透明度都发生变化。当鼠标悬停在其中一个上时它会起作用。但是当鼠标在矩形的交叉区域时,只有上面的矩形改变了它的不透明度。你能告诉我在这种情况下如何让两个矩形都改变不透明度吗?

<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;
}

当然最好为这种情况添加一些特殊的附加属性和行为。

关于wpf - MouseEvent 不冒泡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6907554/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com