简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM