MainWindow.xaml
<Window x:Class="ListBox的使用.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid Name="grid1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="48*" />
<ColumnDefinition Width="455*" />
</Grid.ColumnDefinitions>
<!--因为listBox的数据源是一个Person类对象的集合,DisplayMemberPath="Name"listBox控件要显示行的内容为Person对象的Name属性值,SelectedValuePath="Age"表示获选中行的值的时候获取到的是Age的值-->
<ListBox DisplayMemberPath="Name" SelectedValuePath="Age" Height="100" HorizontalAlignment="Left" Margin="62,78,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" Grid.Column="1">
<ListBox.Items>
<!--<ListBoxItem Content="湖南"></ListBoxItem>-->
<!--<ListBoxItem Content="广东"></ListBoxItem>-->
<!--<ListBoxItem Content="四川"></ListBoxItem>-->
<!--<Button Content="北京"></Button>-->
<!--<ListBoxItem Content="{Binding Name}"></ListBoxItem>-->
<!--<ListBoxItem Content="{Binding Age}"></ListBoxItem>-->
<!--<ListBoxItem Content="{Binding Height}"></ListBoxItem>-->
</ListBox.Items>
</ListBox>
<Button Content="获得listBox选中行的值" Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="62,37,0,0" Name="button1" VerticalAlignment="Top" Width="132" Click="button1_Click" />
<TextBox Grid.Column="1" Height="23" HorizontalAlignment="Right" Margin="0,37,135,0" Name="txtListBoxSelectedValue" VerticalAlignment="Top" Width="120" />
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Model;
namespace ListBox的使用
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
List<Person> list = new List<Person>();
Person p1 = new Person();
p1.Name = "杨中科";
p1.Age = 38;
p1.Height = 172;
list.Add(p1);
//list.Add(new Person(Name = "毛泽东", Age = 76, Height = 184));
Person p2 = new Person();
p2.Name = "周恩来";
p2.Age = 72;
p2.Height = 178;
list.Add(p2);
//这个list里面总共放了有两个Person对象
listBox1.ItemsSource = list;
//因为listBox1控件是绑定Person类的对象集合的。所以我们得让listBox1显示
//Person类对象的某一个属性的值,下面的设置就是让listBox1控件显示Person
//类对象的Name属性值。如果不设的话就会显示为Person类的名字
listBox1.DisplayMemberPath = "Name";
//因为listBox1可以有很多行,一行就是一个Person对象,那么当我选中其中一行的时候,也就是说选中一个对象
//那么现在我要用SelectedValue获得选中这一行的值,可是这一行是一个对象,我应该怎么取呢?于是下面的设置就是“当选中一行的时候,我所取到的值设定为Person类对象的Age属性的值”
listBox1.SelectedValuePath = "Age";
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//SelectedItem获得的是选中行的对应的对象。如果没有选中行就是null
Person selectedItem = listBox1.SelectedItem as Person;
//SelectedValue获得的是选中行对应的对象的“SelectedValuePath="Age"标示的属性”的属性值即Age的值
int selectedValue =(int) listBox1.SelectedValue;
txtListBoxSelectedValue.Text = selectedValue.ToString();
}
}
}
Model类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Model
{
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public int Height { get; set; }
}
}