How to get/specify info about a programmaticaly-ad

2019-08-29 12:37发布

Say I added a series buttons to a WPF app progammatically, as part of a label, textbox, button section, and attached a single handler to the click event of all of these. How can I specify which button (pertaining to which section) is being clicked, so it can be handled accordingly?

Attaching individuals handlers won't work since the user must be able to add as many of these 'lines' as needed.

A possible alternative would be to pass information to the event handler, such as:

    ...
    var sender = this;
    var args = new CustomEventArgs(sectionName);

    var button = new Button();
    button.Click += Button_EventHandler_Click(sender, args);

But I haven't found a way to this in C#.

Any help/idea will be appreciated!

Thanks

1条回答
Root(大扎)
2楼-- · 2019-08-29 12:58

Look at the sender parameter, it will be the clicked button.

If you need to further differenciate the buttons, you can set the Tag property of the button.

查看更多
登录 后发表回答