4

I have ellipses drawn on top of a canvas, I've registered a MouseLeftButtonUp on the Canvas, and I and want to get what ellipse was clicked using:

(Ellipse)e.Source

But e.Source is always the canvas, I also tried e.OriginalSource and also did the same thing with PreviewMouseLeftButtonUp, but still same result. Event arg is MouseButtonEventArgs. What am I doing wrong? also is there a way to check if there is a child element of a canvas at a specific point.

H.B.
  • 166,899
  • 29
  • 327
  • 400
mihajlv
  • 2,275
  • 4
  • 36
  • 59
  • You itself are saying that you registerd the event on Canvas, so the source and original source will always be canvas itself. Can you paste the relevant code here? – Rohit Vats Aug 29 '11 at 17:42
  • @RV1987 why does the same logic work for mouseleftbuttondown? Here is the code: `private void mainCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { MessageBox.Show((e.Source).ToString()); } ` the exact same code shows ellipse in the mouseonleftbuttondown. – mihajlv Aug 29 '11 at 19:20

3 Answers3

2

Use e.OriginalSource instead.

H.B.
  • 166,899
  • 29
  • 327
  • 400
0

You probably want PreviewMouseLeftButtonUp, not MouseLeftButtonUp. Also, you may need to register your event using Ellipse.PreviewMouseLeftButtonUp, rather than the MouseUp event on Canvas. Something like this:

<Canvas ... Ellipse.PreviewMouseLeftButtonUp="MyHandler" />

And as H.B. mentioned, you want to use e.OriginalSource, not e.Source.

EDIT: Misread the question, editing to change to PreviewMouse*LeftButton*Up

Chris Shain
  • 50,833
  • 6
  • 93
  • 125
0

I just ran into this issue and this was the first answer to pop up. It was working for me on MouseLeftButtonDown but not on MouseLeftButtonUp. This was because of a logic error I had made with capturing the mouse in my MouseDown handler.

MouseUp events only fire on the element that is captured if you are capturing the element.

I had the following line in my MouseLeftButtonDown handler:

((FrameworkElement) sender).CaptureMouse();

Changing that to the following fixed the problem:

((FrameworkElement) e.OriginalSource).CaptureMouse();
JVal90
  • 176
  • 4