rae1
rae1

Reputation: 6144

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?

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

Upvotes: 0

Views: 116

Answers (1)

thumbmunkeys
thumbmunkeys

Reputation: 20764

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.

Upvotes: 2

Related Questions