views:

71

answers:

1

As part of a diagram, I am drawing a few overlapping Shapes, each with Opacity=0.5, like here:

<Grid>
    <Rectangle Fill="Blue" Opacity="0.5" MouseEnter="Rectangle_MouseEnter" />
    <Rectangle Fill="Red" Opacity="0.5" />
</Grid>


private void Rectangle_MouseEnter(object sender, MouseEventArgs e)
  {
     MessageBox.Show("Entered");
  }

When the user enters the shape with the mouse, some additional information should be displayed, but the event handler never gets called.

Is there a way to get MouseEnter events for all Shapes, instead of just the topmost one?

+1  A: 

With your layout only the topmost rectangle will raise MouseEnter event. It fully overlaps the first rectangle.

Try this code for eventHandler:

private void Rectangle_MouseEnter(object sender, MouseEventArgs e)
        {
            if (sender != grid.Children[0])
            {
                var rect = (grid.Children[0] as Rectangle);
                if (rect != null) rect.RaiseEvent(e);
            }
            else
            {
                MessageBox.Show("Entered.");
            }
        }

For this works you need to subscribe both rectangles to Rectangle_MouseEnter.

Eugene Cheverda
Yeah, I noticed. =) Is there another way to get notified when the user enters a region that is behind something else?
Jens
I'm afraid that no... MouseEnter and MouseLeave events don't bubble and raise only over exactly that control on which you place your cursor. http://msdn.microsoft.com/en-us/library/cc189029(v=VS.95).aspx#mouse_routed_events
Eugene Cheverda
Check update, may be it will help you.
Eugene Cheverda
Thats looks hacky, but thanks, I'll try that.
Jens