In WPF programming, I have a problem to write the button click event handler. Because the button is inside a listbox item(a part of datatemplate), and when the button is cli
You can get that information from the DataContext
.
public class Person {
public string Name { get; set; }
public int Age { get; set; }
}
Persons = new ObservableCollection() {
new Person() { Name = "John", Age = 30 },
new Person() { Name = "Tim", Age = 48 }
};
lbPersons.DataContext = Persons;
private void person_Clicked(object sender, RoutedEventArgs e) {
Button cmd = (Button)sender;
if (cmd.DataContext is Person) {
Person p = (Person)cmd.DataContext;
}
}
And in XAML: