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?