如何在创建Rectangle [X]后在Rectangle [X]上创建Mouse事件

时间:2017-02-03 11:19:11

标签: c# wpf mouseevent

我的crate矩形代码:

     Rectangle[] myRectangle = new Rectangle[100];
     for(int i=1;i<=100;i++)
     {
          myRectangle[i] = new Rectangle();
          // another code for create myRectangle[i]
     }

当MouseEnter Rectangle [X]和我想做某事时。我该怎么办? 谢谢。

1 个答案:

答案 0 :(得分:0)

把它放在这里;

Rectangle[] myRectangle = new Rectangle[100];
for(int i = 0; i < 100; i++)
{
    myRectangle[i] = new Rectangle();
    myRectangle[i].Tag = i;
    myRectangle[i].MouseEnter += MouseEnter;
}
private void MouseEnter(object sender, MouseEventArgs e)
{
    Rectangle rect = (Rectangle)sender;
    int idx = (int)rect.Tag;
    MessageBox.Show(idx.ToString());
}