MouseEnter event is not always triggered

0 votes
asked Jul 30, 2010 by user341877

In my WPF project I have a canvas on which I draw an ellipse in the XAML and add a MouseEnter event to it:

    <Canvas Width="600" Height="480" Name="canvas1" HorizontalAlignment="Left">

        <Ellipse Height="20" Width="20"  Canvas.Left="50" Canvas.Top="50" Fill="blue" Name="ellipse1" Mouse.MouseEnter="ellipse1_MouseEnter" MouseLeave ="ellipse1_MouseLeave"/>


In the codebehind I've got this code:

    private void ellipse1_MouseEnter(object sender, MouseEventArgs e)
        ellipse1.Fill = Brushes.Red;

When I enter the ellipse with my mouse, it turns red as expected.

I also have code to draw an ellipse on the canvas where I click my mouse. I have a class called Vertex in which I create a ellipse, which has a reference to the canvas.

When I instantiate a new Vertex (and so an ellipse), I add the ellipse to the canvas' children. Before adding it to the canvas, I add a handler to the MouseEnter event:

MyEllipse.MouseEnter += new System.Windows.Input.MouseEventHandler(MyEllipse_MouseEnter);

The "MyEllipse_MouseEnter" handler looks like this:

    private void MyEllipse_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        this.MyEllipse.Fill = Brushes.Red;

I expected this to work as it is the same as the first example which works.

However, when I enter the drawn ellipse with the mouse, my handler is not caleld. BUT, when go back and forth over the ellipse many times, it will eventually trigger and color the ellipse red. But this only happens on one of the many ellipses I draw, which also seems very strange.

What could be causing this strange behaviour?

2 Answers

0 votes
answered Jul 2, 2010 by user341877

Solved the problem!

When drawing the ellipse as a part of a Vertex, I add a label to the ellipse. Better said, I put a label on top of the ellipse:

        Canvas.SetZIndex(myEllipse, 10);
        Canvas.SetLeft(myEllipse, coordinates.X);
        Canvas.SetTop(myEllipse, coordinates.Y);

        Canvas.SetZIndex(myLabel, 10);
        Canvas.SetLeft(myLabel, coordinates.X - 1);
        Canvas.SetTop(myLabel, coordinates.Y - 5);


So when I clicked an ellipse on the canvas I actually clicked the label rather then the ellipse. The solution for his was simple:

        myLabel.IsHitTestVisible = false;

Now the label cannot be hitted :D

Thx everbody!

0 votes
answered Jul 31, 2010 by chris-taylor

Can you share the code you are using to programatically draw the ellipse. I suspect that your programatic ellipse is not filled initially making it transparent, the transparent region is not treated as part of the ellipse and the messages are therefore not raised to the ellipse.

If my abovew assumption is correct, the occasions when you do get the messages, it is when the mouse pointer hits the outline of the ellipse.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter