Hi all!
I've got a weird problem. Forgive me if I'm mixing up terms but I'm still a beginner ;)
The situation:
In my WPF project I've got a canvas on which I draw Ellipse. I create the 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"/>
</Canvas>
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, the Ellipse turns Red. So this works as it should be.
This is actually a test in the project where I want to do the same, only at runtime.
I've made functionality which draws an Ellipse on the canvas where I click my mouse. To get this done I've got 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 an handler to the MouseEnter event (is this the right way to say it? -> "add a handler to an 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;
}
This all looks good to me since it is the same as the previous test which works. But unfortunately is doesn't work right. When I enter the drawed Ellipse with the mouse, the event does not raise (or should I say "bubbles" ?). BUT, when go back and forth over the Ellipse many times, it will eventually rais and color the Ellipse red. But this only happens on one of the many Ellipses I draw, which also seems very strange to me.
So can somebody tell me the reasons for this strange behaviour?
Thanks!