using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Text; using System.Windows.Forms; namespace KK.Control.Helper { public class CmbHelper { //定义一个用于保存静态变量的实例 private static CmbHelper instance = null; //定义一个保证线程同步的标识 private static readonly object locker = new object(); //构造函数为私有,使外界不能创建该类的实例 private CmbHelper() { } public static CmbHelper Instance { get { if (instance == null) { lock (locker) { if (instance == null) instance = new CmbHelper(); } } return instance; } } public TModel GetTModel<TModel>(ComboBox cmb) { //ZB.QueueSys.Model.QS_QUEUE queue = this.cmbQueue.SelectedItem as ZB.QueueSys.Model.QS_QUEUE; //StudentInfos stu = CmbHelper.Instance.GetTModel<StudentInfos>(this.cmbTest); return (TModel)cmb.SelectedItem; } public void BindingCmb<TModel>(List<TModel> list, ComboBox cmb, string displayMember) { if (list == null || list.Count <= 0) { cmb.DataSource = null; return; } cmb.DataSource = list; cmb.DisplayMember = displayMember; cmb.SelectedIndex = 0; //List<V_APPLYDEPART> departs = AppDepartBLL.GetApplyDeparts(); //if (departs != null && departs.Count > 0) departs.Insert(0, new V_APPLYDEPART { APPLYDEPART = PubVariable.All, DEPARTID = "-1" }); //PubHelper.Instance.BindingCmb(departs, this.cmbDepart, "APPLYDEPART"); } public void BindingCmbAndInsetAll<TModel>(List<TModel> list, ComboBox cmb, string displayMember, string all = "全部") { if (list == null || list.Count <= 0) { cmb.DataSource = null; return; } cmb.Items.Clear(); cmb.Items.Add(all); foreach (TModel item in list) { cmb.Items.Add(item); } //cmb.DataSource = list; cmb.DisplayMember = displayMember; cmb.SelectedIndex = 0; //PubHelper.Instance.BindingCmb(departs, this.cmbDepart, "APPLYDEPART"); } /// <summary> /// 为ComboBox绑定数据源并提供下拉提示 /// </summary> /// <typeparam name="T">泛型</typeparam> /// <param name="combox">ComboBox</param> /// <param name="list">数据源</param> /// <param name="displayMember">显示字段</param> /// <param name="valueMember">隐式字段</param> /// <param name="displayText">下拉提示文字</param> public void Bind<T>(ComboBox combox, IList<T> list, string displayMember, string valueMember, string displayText = "全部") { AddItem(list, displayMember, displayText); combox.DataSource = list; combox.DisplayMember = displayMember; if (!string.IsNullOrEmpty(valueMember)) combox.ValueMember = valueMember; } private void AddItem<T>(IList<T> list, string displayMember, string displayText) { Object _obj = Activator.CreateInstance<T>(); Type _type = _obj.GetType(); if (!string.IsNullOrEmpty(displayMember)) { PropertyInfo _displayProperty = _type.GetProperty(displayMember); _displayProperty.SetValue(_obj, displayText, null); } list.Insert(0, (T)_obj); } } }
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!