如何让 ListBox 控件绑定到自定义数据结构并显示自定义数据结构中的指定数据?
如何让 ListBox 控件绑定到自定义数据结构并显示自定义数据结构中的指定数据?
例如,一个学生有姓名和年龄两个属性,一个班级有50名学生。我们要用一个 ListBox 控件采用数据绑定的方式显示数据,并且只显示这个班级每个学生的姓名。
数据绑定到自定义数据结构的关键点在于绑定字段必须是属性(而非变量)。在本例中,我们要让控件 listBox1 显示“姓名字段”,所以“Name”必须声明为一个属性,而不能是变量。
本文章搜索引擎关键字:WinForm、WindowsForm、数据绑定、自定义数据绑定、自定义数据结构、自定义数据结构绑定到 ListBox 控件。
下面是相关代码实现:(.NET Framework 2.0 环境编译通过)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace test
{
public class Form1 : Form
{
/// <summary>
/// 你自己的数据结构。在这个例子中,我们定义了“姓名”和“年龄”两个属性。
/// </summary>
public class Stu
{
private string _Name;
/// <summary>
/// 姓名
/// </summary>
public string Name
{
get
{
return this._Name;
}
set
{
this._Name = value;
}
}
private int _Age;
/// <summary>
/// 年龄
/// </summary>
public int Age
{
get
{
return this._Age;
}
set
{
this._Age = value;
}
}
public Stu(string pName, int pAge)
{
this.Name = pName;
this.Age = pAge;
}
}
/// <summary>
/// 这里定义了一个数据集合用来存放类“Stu”的实例。
/// </summary>
System.Collections.Generic.List<Stu> myClass = new List<Stu>();
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
public Form1()
{
InitializeComponent();
myClass.Add(new Stu("赵志良", 26));
myClass.Add(new Stu("马玉华", 33));
myClass.Add(new Stu("王贺", 30));
this.listBox1.DataSource = myClass;//绑定数据。
this.listBox1.DisplayMember = "Name";//指示列表控件显示“姓名”属性。
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(12, 22);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(408, 232);
this.listBox1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(134, 281);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(162, 23);
this.button1.TabIndex = 1;
this.button1.Text = "显示选中记录的详细信息";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(432, 338);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, EventArgs e)
{
if (this.listBox1.SelectedIndex == -1)
{
MessageBox.Show("必须选中一条记录。");
return;
}
Stu s = (Stu)(this.listBox1.SelectedItem);
MessageBox.Show("姓名:" + s.Name + "\r\n年龄:" + s.Age);
}
}
}