Wpf MouseEvent没有冒泡

Wpf MouseEvent没有冒泡,wpf,silverlight,mouseevent,styles,Wpf,Silverlight,Mouseevent,Styles,我有两个相交的矩形。我希望当鼠标悬停在它们上面时,两者的不透明度都会改变。当鼠标在其中一个上面时,它就工作了。但是,当鼠标位于矩形的相交区域时,只有上部矩形更改其不透明度。你能让我知道在这种情况下,如何让两个矩形都改变不透明度吗 <Window x:Class="WpfTestApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://sc

我有两个相交的矩形。我希望当鼠标悬停在它们上面时,两者的不透明度都会改变。当鼠标在其中一个上面时,它就工作了。但是,当鼠标位于矩形的相交区域时,只有上部矩形更改其不透明度。你能让我知道在这种情况下,如何让两个矩形都改变不透明度吗

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

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

如果必须基于样式,Mario Vernari的建议将起作用,但悬停将在网格中的任何位置触发,而不仅仅是在矩形上


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

尝试将样式应用于网格元素。

事实上,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;
    }
private List hitsresultslist=new List();
私有无效布局root\u MouseMove(对象发送方,MouseEventArgs e)
{
//检索鼠标位置的坐标。
点pt=e.GetPosition((UIElement)发送器);
//清除用于命中测试结果的列表的内容。
hitResultsList.Clear();
//设置回调以接收命中测试结果枚举。
VisualTreeHelper.HitTest(LayoutRoot,null,
新HitTestResultCallback(MyHitTestResult),
新的PointHitTestParameters(pt));
//解除所有孩子的职务
for(int i=0;i

当然,最好在此案例中添加一些特殊的附加属性和行为。

感谢HiTech的回复。我更倾向于只使用xaml的解决方案。你能推荐一些吗?如果不可能,我将尝试此方法。@Souvik Basu:Xaml仅意味着您需要通过一些新方法模拟现有事件。我会尝试创建一个行为来生成事件(或状态更改)。感谢Kreol的回复。正如我在对HiTech的评论中提到的,我更倾向于只使用xaml的解决方案。如果你能建议一个只使用xaml的解决方案,那就太好了,否则我会试试这个。不幸的是,我想这个任务没有“只使用xaml”的解决方案。因此,我建议您——将所有这些代码分离到容器控件的特殊附加属性(行为),当然,添加一些附加属性,而不是我的示例中使用的标记。这样,您就可以使用XAML中的这些附加属性,而无需代码隐藏。感谢Mario的回复。但这不符合我的目的,因为我只想在鼠标悬停在矩形上而不是网格上时突出显示矩形。网格没有背景色(即null),因此不会捕获所有鼠标事件。如果尝试将网格悬停在矩形之外,则不会出现任何褪色(无事件)。但是,除非将背景保持为空,“透明”是不同的!注意:我已经在WPF上试过了,效果非常好。但是,我没有尝试使用Silverlight。当我在网格上设置样式而不是矩形时,当我将鼠标悬停在其中任何一个矩形上时,这两个矩形都会高亮显示。如果我将鼠标悬停在任何特定的矩形上,我只希望该矩形高亮显示。如果我把鼠标悬停在十字路口,我希望两者都突出显示。噢!。。。我误解了你的目标,对不起!