首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MouseEvent没有冒泡

MouseEvent没有冒泡
EN

Stack Overflow用户
提问于 2011-08-02 13:15:43
回答 3查看 449关注 0票数 3

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

代码语言:javascript
复制
<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>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-08-02 16:48:50

事实上,HiTech魔术所描述的类似方法:

代码语言:javascript
复制
<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>

和它背后的代码:

代码语言:javascript
复制
    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;
    }

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

票数 2
EN

Stack Overflow用户

发布于 2011-08-02 15:55:49

您需要将悬停处理程序添加到父网格中,使两个矩形都具有IsHitTestVisible=False,并使用VisualTreeHelper.FindElementsInHostCoordinates来确定鼠标(1或更多)下的实际对象。

如果你必须让它基于样式,Mario Vernari的建议是可行的,但悬停将触发网格中的任何地方,而不仅仅是矩形。

这对我来说是一个非常有用的附加行为的想法。该行为将实现上面概述的代码,但会在子对象上触发悬停事件,因此您仍然可以使用样式...将不得不试一试。

票数 1
EN

Stack Overflow用户

发布于 2011-08-02 15:59:02

尝试将样式应用于Grid元素。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6907554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档