Hello,
To start off, this is not a homework related question, just something I've run into on a project I'm working on for myself for fun.

Basically, I have a screen whose controls are dynamically created (I create one for each root entry in an xml file) so that the content of the screen can be easily changed - or at least that's the idea.

Where I'm running into problems is creating the event handlers for when a user clicks on the controls. Depending on which on is clicked, different data will be displayed elsewhere on the screen. I know how to add generic "onClicked" handlers to buttons, or any type of control, but I can't seem to figure out how to get it to work correctly when everything is dynamically created.

As an example, I have:

public class Person{
public string Name {get; set;}
public int Age {get; set;}
public string HomeTown {get; set;}
//... etc.

//then, where my screen is created,
Button temp;
foreach (Person p in people){//people is a list of Persons created by reading the xml file(s)
temp = new Button();
temp.Content = p.Name;
temp.Click += new RoutedEventHandler(personButton_Click);
//then add it to the screen, etc.
//...
}
//...
private void personButton_Click(object sender, RoutedEventArgs e){
//this is what I can't figure out
//I want to set the other controls on the screen to the items specific to the Person that corresponds with the Button that was Clicked.
//For this example, if a button labeled "John" was clicked, then the "Age" and "HomeTown" textboxes on the screen would show John's age and home town. If "Jane" was clicked, then Jane's age and home town would appear in those same textboxes.
//if I created the buttons statically, like physically create one for each person, I could easily add a separate event handler for each button that sets the other controls correctly, but this is not what I want to do.
}

I hope I was able convey what I'm trying to do. If anyone has any suggestions it would be greatly appreciated!

Thanks
-oredigger

You have the sender object, cast it to a button and look at the Text property and do whatever you need to look up the age, hometown, etc.

You have the sender object, cast it to a button and look at the Text property and do whatever you need to look up the age, hometown, etc.

I was wondering how you use the sender object... should have thought about casting it *Bangs Head*

Thanks!

This question has already been answered. Start a new discussion instead.