How to get/specify info about a programmaticaly-added button in WPF? Or similarly, how to pass custom EventArgs to such a button click event?

你说的曾经没有我的故事 提交于 2019-12-12 01:45:16

问题


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:


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.



来源:https://stackoverflow.com/questions/9571974/how-to-get-specify-info-about-a-programmaticaly-added-button-in-wpf-or-similarl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!