Wpf的comboBox怎么绑定数据? 10
Wpf的comboBox怎么绑定数据?数据是从数据库读取出来的,显示的是类别的名称,点击提交时提交的是类别的ID,这要怎么做?...
Wpf的comboBox怎么绑定数据?数据是从数据库读取出来的,显示的是类别的名称,点击提交时提交的是类别的ID,这要怎么做?
展开
3个回答
推荐于2017-09-30 · 知道合伙人互联网行家
关注
展开全部
WPF中提供了数据绑定的功能,操作起来很方便,集合类的控件几乎都可以用数据源来进行数据的绑定,下面操作一下下拉列表框控件ComboBox控件的数据绑定操作。
要绑定到ComboBox控件的自定义类:
public class LocationRoad
{
public int ID { set; get; }
public string Code { set; get; }
public string Info { set; get; }
}
建立数据源,就将此数据集合当作数据源绑定到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控件的自定义类:
public class LocationRoad
{
public int ID { set; get; }
public string Code { set; get; }
public string Info { set; get; }
}
建立数据源,就将此数据集合当作数据源绑定到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 Name="fooBarComboBox"
ItemsSource="{Binding Path=ExampleEnumsWithCaptions}"
DisplayMemberPath="Value"
SelectedValuePath="Key"
SelectedValue="{Binding Path=ExampleProperty, Mode=TwoWay}" >
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询