The mouse events are declared in the UIElement class. When we want interactive shapes in the UI we usually put elements that derive from Shape (e.g. Ellipse, Rectangle, Path etc...).
If you want an interactive shape, you can create a Shape
and add it to the Children of the Canvas
, as the following:
Ellipse el = new Ellipse
{
Width = 100,
Height = 50,
Fill = Brushes.Green
};
Canvas.SetTop(el, 30);
Canvas.SetLeft(el, 40);
myCanvas.Children.Add(el);
Then you can create the event-handler:
void OnEllipseMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
}
and, register to the shape's event:
el.MouseLeftButtonDown += OnEllipseMouseLeftButtonDown;
If you want to handle events for shapes that is drawn using DrawingContext
, you can store the details of the shapes (like bounding rectangle) and, use these details in the event-handlers of the Canvas
's events, in order to find which shape is clicked.