WPF—— ListBox的使用
2014-05-19 15:48
183 查看
MainWindow.xaml
MainWindow.xaml.cs
Model类
<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; } } }
相关文章推荐
- WPF 使用鼠标滚轮控制ListBox水平滚动条滚动
- Wpf控件ListBox使用实例2
- WPF中Listbox使用
- 【WPF】ListBox使用DataTemplate 以及默认选中第一项Item
- 基于modern ui for wpf的在线公开课平台 之五 使用ListBox实现图片列表效果
- Wpf控件ListBox使用实例2
- WPF中Listbox使用
- 【WPF】ListBox使用UserContrl作为子控件
- 如何在sap的screen中使用ListBox
- WPF样式中使用BasedOn 注意顺序问题
- 【Vegas原创】使用javascript实现两个listbox中的添加、删除
- 使用CAknIconArray读取资源图标供listbox所用
- Windowsx.h使用之ListBox control message APIs
- 如何在sap的screen中使用ListBox
- 使用 WPF 向控件和三维表面添加视频
- listbox 使用笔记
- 在 WPF 窗口内部使用 Aero 效果
- 使用WPF实现3D场景[二]
- ListBox 在Javascript 中的使用
- WPF中使用Style设置界面控件外观属性