1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows;
6 using System.Windows.Controls;
7 using System.Windows.Data;
8 using System.Windows.Documents;
9 using System.Windows.Input;
10 using System.Windows.Media;
11 using System.Windows.Media.Imaging;
12 using System.Windows.Navigation;
13 using System.Windows.Shapes;
14
15 namespace ListBox
16 {
17 /// <summary>
18 /// MainWindow.xaml 的交互逻辑
19 /// </summary>
20 public partial class MainWindow : Window
21 {
22 public MainWindow()
23 {
24 InitializeComponent();
25 }
26
27 private void Window_Loaded(object sender, RoutedEventArgs e)
28 {
29 List<Person> list = new List<Person>();
30 Person p1 = new Person();
31 p1.Name = "张三";
32 p1.Age = 18;
33 list.Add(p1);
34 list.Add(new Person(){Name="王五",Age=19});
35 list.Add(new Person() { Name = "赵柳", Age = 19 });
36
37 //ListBox显示的集合是ItemsSource属性,不是DataContent
38 lbPersons.ItemsSource = list;
39
40
41
42 }
43
44 private void button1_Click(object sender, RoutedEventArgs e)
45 {
46 //SelectedItem获得的是选中行对应的对象
47 object selectedItem = lbPersons.SelectedItem;
48 //SelectedValue获得的是选中行对应的对象的“SelectedValuePath”标记的属性值
49 object selectedValue = lbPersons.SelectedValue;
50 }
51 }
52 }