I define parts of a menu with an array of string, and I want to automatically create click events for each of these. All of them are going to to the same thing, only a different parameter.
string[] goTos = new string[] { "First", "Second", "Third" };
When Initializing the window:
foreach (string item in goTos)
{
System.Windows.Controls.MenuItem goTo = new System.Windows.Controls.MenuItem();
goTo.Header = item;
goTo.Name = "mnu" + item;
mnuGoTo.Items.Add(goTo);
}
How can I get click events for these?
MenuItemcalled the handler by examining thesenderparameter of your handler method.senderobject passed to your handler method is theMenuItemobject. So if you use(sender as MenuItem).Headeryou've got all you want. (You may need to add a check for null if the handler might get called from other controls too.)