绑定的代码 :
private void bindType()
{
Code.Dao.XMType xt = new Code.Dao.XMType();
System.Data.DataTable dt = xt.ListDB();
cmbType.ItemsSource = dt.DefaultView;
cmbType.DisplayMemberPath = "name";
cmbType.SelectedIndex = 0;
}
设置选择项:
System.Data.DataView dv = (System.Data.DataView)cmbType.ItemsSource;
System.Data.DataTable dt = dv.ToTable();
for (int i = 0; i < dt.Rows.Count; i++)
{
if (selItem.Type.ID == int.Parse(dt.Rows[i]["id"].ToString()))
{
cmbType.SelectedIndex = i;
break;
}
}
获取选择项:
int id = int.Parse((cmbType.SelectedItem as System.Data.DataRowView).Row["id"].ToString());
string typename = (cmbType.SelectedItem as System.Data.DataRowView).Row["name"].ToString();
这是绑定的数据表,如果绑定的集合,代码如下:
Hos.Manager.OfficeAndPerson.Office o = new Hos.Manager.OfficeAndPerson.Office();
List<Hos.Model.OfficeAndPerson.Office> list = o.List(_type);
cmbOffice.DataContext = list;
cmbOffice.SelectedIndex = 0;
设置选择项:
List<Hos.Model.OfficeAndPerson.Office> list=(List<Hos.Model.OfficeAndPerson.Office>)cmbOffice.DataContext;
for (int i = 0; i < list.Count; i++)
{
Hos.Model.OfficeAndPerson.Office tem = list[i];
if (tem.Id == info.Id)
{
cmbOffice.SelectedIndex = i;
break;
}
}
获取选择项:
Hos.Model.OfficeAndPerson.Office office = cmbOffice.SelectedItem as Hos.Model.OfficeAndPerson.Office;