wpf之ComboBox绑定
方式一、通过泛型Dictionary绑定
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
Dictionary<int, string> mydic = new Dictionary<int, string>() {
{0,"aaaa"},
{1,"bbbb"},
{2,"cccc"}
};
cobxUserType.ItemsSource = mydic;
cobxUserType.SelectedValuePath = "Key";
cobxUserType.DisplayMemberPath = "Value";
{0,"aaaa"},
{1,"bbbb"},
{2,"cccc"}
};
cobxUserType.ItemsSource = mydic;
cobxUserType.SelectedValuePath = "Key";
cobxUserType.DisplayMemberPath = "Value";
然后通过cobxUserType.SelectedValue获得选中的值即可
方法二、通过数据集绑定
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
WebDictClassServices.WebDictClass webDict = new WebDictClass();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
BindCombox();
BindCombox2(comboBox2,0,"选择系列");
BindCombox2(comboBox3,0,"选择型号");
}
//绑定下拉控件1
private void BindCombox()
{
DataTable dt = webDict.GetMainBrand();
if (dt != null && dt.Rows.Count == 0)
{
dt = new DataTable();
DataColumn dc1 = new DataColumn("Title");
DataColumn dc2 = new DataColumn("ID");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
DataRow row = dt.NewRow();
row["title"] = "按品牌选择";
row["ID"] = "0";
dt.Rows.Add(row);
}
else
{
DataRow row = dt.NewRow();
row["title"] = "按品牌选择";
row["ID"] = "0";
dt.Rows.Add(row);
}
DataView dv=dt.DefaultView;
dv.Sort=" ID asc";
combMainBrand.ItemsSource =dv;
this.combMainBrand.DisplayMemberPath = "Title";
this.combMainBrand.SelectedValuePath = "ID";
combMainBrand.SelectedValue = "0";
}
//绑定下拉控件
private void BindCombox2(ComboBox comb, int id,string message)
{
DataTable dt = null;
if (id == 0)
{
dt = new DataTable();
DataColumn dc1 = new DataColumn("Title");
DataColumn dc2 = new DataColumn("ID");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
DataRow row = dt.NewRow();
row["Title"] = message;
row["ID"] = "0";
dt.Rows.Add(row);
}
else
{
dt = webDict.GetCarSeriesByBrand(id);
DataRow row = dt.NewRow();
row["Title"] = message;
row["ID"] = "0";
dt.Rows.Add(row);
}
DataView dv = dt.DefaultView;
dv.Sort = " ID asc";
comb.ItemsSource = dv;
comb.DisplayMemberPath = "Title";
comb.SelectedValuePath = "ID";
if (dt.Rows.Count!=0)
{
comb.SelectedValue = dt.Rows[0]["ID"];
}
}
private void combMainBrand_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int id = Convert.ToInt32(combMainBrand.SelectedValue);
BindCombox2(comboBox2, id, "选择系列");
BindCombox2(comboBox3, Convert.ToInt32(comboBox2.SelectedValue), "选择型号");
}
private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int id = Convert.ToInt32(comboBox2.SelectedValue);
BindCombox2(comboBox3, id, "选择型号");
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
BindCombox();
BindCombox2(comboBox2,0,"选择系列");
BindCombox2(comboBox3,0,"选择型号");
}
//绑定下拉控件1
private void BindCombox()
{
DataTable dt = webDict.GetMainBrand();
if (dt != null && dt.Rows.Count == 0)
{
dt = new DataTable();
DataColumn dc1 = new DataColumn("Title");
DataColumn dc2 = new DataColumn("ID");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
DataRow row = dt.NewRow();
row["title"] = "按品牌选择";
row["ID"] = "0";
dt.Rows.Add(row);
}
else
{
DataRow row = dt.NewRow();
row["title"] = "按品牌选择";
row["ID"] = "0";
dt.Rows.Add(row);
}
DataView dv=dt.DefaultView;
dv.Sort=" ID asc";
combMainBrand.ItemsSource =dv;
this.combMainBrand.DisplayMemberPath = "Title";
this.combMainBrand.SelectedValuePath = "ID";
combMainBrand.SelectedValue = "0";
}
//绑定下拉控件
private void BindCombox2(ComboBox comb, int id,string message)
{
DataTable dt = null;
if (id == 0)
{
dt = new DataTable();
DataColumn dc1 = new DataColumn("Title");
DataColumn dc2 = new DataColumn("ID");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
DataRow row = dt.NewRow();
row["Title"] = message;
row["ID"] = "0";
dt.Rows.Add(row);
}
else
{
dt = webDict.GetCarSeriesByBrand(id);
DataRow row = dt.NewRow();
row["Title"] = message;
row["ID"] = "0";
dt.Rows.Add(row);
}
DataView dv = dt.DefaultView;
dv.Sort = " ID asc";
comb.ItemsSource = dv;
comb.DisplayMemberPath = "Title";
comb.SelectedValuePath = "ID";
if (dt.Rows.Count!=0)
{
comb.SelectedValue = dt.Rows[0]["ID"];
}
}
private void combMainBrand_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int id = Convert.ToInt32(combMainBrand.SelectedValue);
BindCombox2(comboBox2, id, "选择系列");
BindCombox2(comboBox3, Convert.ToInt32(comboBox2.SelectedValue), "选择型号");
}
private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int id = Convert.ToInt32(comboBox2.SelectedValue);
BindCombox2(comboBox3, id, "选择型号");
}
三、IList绑定(类似winForm)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class Info
{
public int Id { get; set; }
public string Name { get; set; }
}
{
public int Id { get; set; }
public string Name { get; set; }
}
//绑定用户类型
private void bindUserType()
{
IList<Info> list = new List<Info>() {
new Info(){ Id=0, Name="供应商"}, new Info(){
Id=1, Name="bbbbbb"}
};
cobxUserType.ItemsSource = list;
cobxUserType.SelectedValuePath = "Id";
cobxUserType.DisplayMemberPath = "Name";
}
private void bindUserType()
{
IList<Info> list = new List<Info>() {
new Info(){ Id=0, Name="供应商"}, new Info(){
Id=1, Name="bbbbbb"}
};
cobxUserType.ItemsSource = list;
cobxUserType.SelectedValuePath = "Id";
cobxUserType.DisplayMemberPath = "Name";
}
多思考,多创新,才是正道!