请问如何实现 点击ListBox的项中的Button,获取Model中的属性?
先上代码:
XAML:
<ListBox x:Name="listBox"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Margin="12"> <TextBlock Text="{Binding Name,StringFormat=姓名:\{0\}}" Foreground="Orange"/> <TextBlock Text="{Binding Age,StringFormat=年龄:\{0\}}" Foreground="Gray"/> <Button Content="获取学生ID"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate></ListBox>
//modelpublic class Student{ public string Name { get; set; } public int Age { get; set; } public int ID { get; set; }} 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}, new Student{Name = "st4",Age = 24,ID = 4}, new Student{Name = "st5",Age = 25,ID = 5}, new Student{Name = "st6",Age = 26,ID = 6}, new Student{Name = "st7",Age = 27,ID = 7}, new Student{Name = "st8",Age = 28,ID = 8}, new Student{Name = "st9",Age = 29,ID = 9}, }; listBox.ItemsSource = students;