views:

91

answers:

2

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!

A: 

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.

Chris Taylor
A: 

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

        canvas.Children.Add(myEllipse);
        canvas.Children.Add(myLabel);

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!