实例一:显示省市
第一步:在MainWindow中托两个ListBox分别命名为lbProv和lbCity
第二步:为主窗体添加loaded事件,代码如下:
1 private void Window_Loaded(object sender, RoutedEventArgs e) 2 { 3 List<string> listProv = new List<string>(); 4 listProv.Add("北京"); 5 listProv.Add("河北"); 6 listProv.Add("河南"); 7 lbProv.ItemsSource = listProv; 8 }
第三步,因为在点击lbProv中选中的省份时候时候要求lbCity显示出不同的城市,所以为lbProv添加SelectionChanged事件,代码如下:
1 private void lbProv_SelectionChanged(object sender, SelectionChangedEventArgs e) 2 { 3 string prov = (string)lbProv.SelectedItem; 4 // MessageBox.Show(prov); 5 if (prov == "北京") 6 { 7 List<string> listCity = new List<string>(); 8 listCity.Add("昌平"); 9 listCity.Add("朝阳"); 10 listCity.Add("海淀"); 11 lbCity.ItemsSource = listCity; 12 } 13 else if (prov == "河北") 14 { 15 List<string> listCity = new List<string>(); 16 listCity.Add("石家庄"); 17 listCity.Add("邯郸"); 18 listCity.Add("保定"); 19 lbCity.ItemsSource = listCity; 20 } 21 else if (prov == "河南") 22 { 23 List<string> listCity = new List<string>(); 24 listCity.Add("安阳"); 25 listCity.Add("郑州"); 26 listCity.Add("新乡"); 27 lbCity.ItemsSource = listCity; 28 } 29 }
第四部,预览即可
实例二:显示学生成绩
第一步:新建一个窗体命名为StudentWindow,并拖放两个ListBox,一个ListBox命名为lbStudents用于存放学生名,一个为lbScore存放分数。
(注意:若在同一个解决方案中的同一项目中,想要更改启动的窗体,则需要在APP.xaml中修改 StartupUri="MainWindow.xaml"的链接,比如此时将修改为StartupUri="StudentWindow.xaml")
第二步:新建一个Student的类,为Student.cs。代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ProvCity { class Student { private string name; private int age; private int score; public string Name { get; set; } public int Age { get; set; } public int Score { get; set; } } }
第三步:为StudentWindow窗体添加Loaded事件,代码如下:
1 private void Window_Loaded(object sender, RoutedEventArgs e) 2 { 3 List<Student> list = new List<Student>(); 4 list.Add(new Student(){Name="张三",Age=15,Score=60});//若存在无参的构造函数则是要有小括号的。 5 list.Add(new Student { Name = "李四", Age = 19, Score =80 }); 6 list.Add(new Student { Name = "王五", Age = 20, Score = 90 }); 7 //关联list控件lbStudent和list中的Item,ListBox显示的集合是ItemsSource属性,不是DataContext 8 lbStudent.ItemsSource = list; 9 10 11 }
预览效果如下:
在lbStudent中显示的是如何在其中显示Student的一个个的对象。若是想要显示Student类对象的名字属性则需要在lbStudent的ListBox中添加DisplayMemberPath="Name"并且添加他的SelectionChanged事件,代码如下:
1 private void lbStudent_SelectionChanged(object sender, SelectionChangedEventArgs e) 2 { 3 Student student = (Student)lbStudent.SelectedItem; 4 // MessageBox.Show(student.Score.ToString()); 5 List<int> listScore=new List<int>(); 6 listScore.Add(student.Score); 7 lbScore.ItemsSource = listScore; 8 9 }
1 <ListBox Height="100" DisplayMemberPath="Name" HorizontalAlignment="Left" Margin="0,23,0,0" Name="lbStudent" VerticalAlignment="Top" Width="120" SelectionChanged="lbStudent_SelectionChanged" />
若是将lbScore的listBox添加DisplayMemberPath="Score"则事件代码也可以为如下:
1 private void lbStudent_SelectionChanged(object sender, SelectionChangedEventArgs e) 2 { 3 Student student = (Student)lbStudent.SelectedItem; 4 // MessageBox.Show(student.Score.ToString()); 5 // List<int> listScore=new List<int>(); 6 List<Student> listScore = new List<Student>(); 7 // listScore.Add(student.Score); 8 listScore.Add(student); 9 lbScore.ItemsSource = listScore; 10 } 11 12 <ListBox Height="100" DisplayMemberPath="Score" HorizontalAlignment="Left" Margin="146,23,0,0" Name="lbScore" VerticalAlignment="Top" Width="120" />