How to define an event handler of MouseLeftButtonDown of program generated Ellipse element of Canvas in WPF?

StackOverflow https://stackoverflow.com/questions/19134522

Domanda

How to define an event handler of MouseLeftButtonDown of program generated Ellipse element of Canvas in WPF? I have something like this:

canvas.Children.Add(new Ellipse(){
    Name = "FirstEllipse",
    Width = 150,
    Height = 100,
    Margin = new Thickness(200, 150, 0, 0),
    Fill = Brushes.Red,
    MouseLeftButtonDown = "Ellipse_MouseLeftButtonDown"
});
È stato utile?

Soluzione

MouseLeftButtonDown is not a property but an event. You register a callback to the event using the += operator:

var e = new Ellipse { 
              Name = "FirstEllipse", 
              Width = 150, 
              Height = 100, 
              Margin = new Thickness(200, 150, 0, 0), 
              Fill = Brushes.Red };
e.MouseLeftButtonDown += Ellipse_MouseLeftButtonDown;
canvas.Children.Add(e);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top