我有一个列表框和绑定学生,我想在单击“Btn”时获取“ID”。但是,我不知道该怎么做。请告诉我该怎么做?
XAML:
<ListBox x:Name="listBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="12">
<TextBlock Text="{Binding Name,StringFormat=Name:\{0\}}"
Foreground="Orange"/>
<TextBlock Text="{Binding Age,StringFormat=Age:\{0\}}"
Foreground="Gray"/>
<Button Content="Get ID Of Student"
x:Name="Btn"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
C#:
List<Student> students = new List<Student>
{
new Student{Name = "st1",Age = 20,ID = 1},
new Student{Name = "st2",Age = 18,ID = 2},
new Student{Name = "st3",Age = 21,ID = 3},
};
listBox.ItemsSource = students;
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public int ID { get; set; }
}