
combobox.Items.Clear()
dtTable = new DataTable()
strSQL = "select 栏位 from 表"
strResult = DBLink.executeQuery(strSQL, dtTable)
if (!strResult.Equals(""))
{
MessageBox.Show("查询时发生异常!")
}
else
for (int i = 0i <= dtTable.Rows.Count - 1i++)
combobox.Items.Add(dtTable.Rows[i][0].ToString())
dtTable.Dispose()
dtTable = null。
WPF中提供了数据绑定的功能, *** 作起来很方便,集合类的控件几乎都可以用数据源来进行数据的绑定,下面 *** 作一下下拉列表框控件ComboBox控件的数据绑定 *** 作。
要绑定到ComboBox控件的自定义类:
public class LocationRoad
{
public int ID { setget}
public string Code { setget}
public string Info { setget}
}
建立数据源,就将此数据集合当作数据源绑定到ComboBox:
///
/// 当ComboBox选中项更改时发生
///
private LocationRoad _selectLocation
public LocationRoad SelectLocation
{
get
{
return this._selectLocation
}
set
{
this._selectLocation = value
if (this.PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("SelectLocation"))
}
}
private ObservableCollection _locationRoad = null
public ObservableCollection LocationSource
{
get
{
if (this._locationRoad == null)
{
this._locationRoad = new ObservableCollection() {
new LocationRoad() { ID = 1, Code = "NGQ", Info = "南岗区" },
new LocationRoad() { ID = 2, Code = "DLQ", Info = "道里区" },
new LocationRoad() { ID = 3, Code = "DWQ", Info = "道外区" },
new LocationRoad() { ID = 4, Code = "PFQ", Info = "平房区" },
new LocationRoad() { ID = 5, Code = "XFQ", Info = "香坊区" },
}
}
return this._locationRoad
}
set
{
this._locationRoad = value
if (this.PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("LocationSource"))
}
}
Combobox控件用于在一组列表中选择其中的一项或多项。
使用方法如下:
AddItem 向列表框增加一项数据。
ListX. AddItem(Item As String)
RemoveItem删除第i项
ListX. RemoveItem(i As Integer)
使用属性如下:
Text表示当前 *** 作项内容。
ListCount表示当前列表框中总数据项数。
ListIndex表示当前 *** 作项下标,第1项=0。
List(i) 表示第i项表项内容。
MultiSelect表示是否允许多选择。
Selected(i)表示第i项是否被选中。
SelCount表示被选中的项数。
Sort表示是否排序。
扩展资料
ComboBox 控件和ListBox 控件在功能上很相似,很多情况下,这两个控件是可以互换使用的,但是还是有某种特定的环境下只适合使用一种控件的情况。
通常,ComboBox控件适合于建议用户选择控件所列举的选项、同时又可以让用户自行在文本框中输入列表中不存在的选项的情况;而ListBox 控件适合于限制用户只能选择列表中的选项的情况。
在用户界面上,因为ComboBox 控件默认情况下是存在下拉列表框的,所以比ListBox 控件占用的窗体空间少,更加适合于使用在存在大量列表项的情况下。ComboBox 控件的比ListBox 控件更加能灵活多用。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)