0

如何MouseLeftButtonDown在 WPF 中定义程序生成的 Canvas 的 Ellipse 元素的事件处理程序?我有这样的事情:

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

1 回答 1

2

MouseLeftButtonDown不是属性而是事件。您使用运算符注册事件回调+=

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);
于 2013-10-02T10:12:43.430 回答