轉(zhuǎn)帖|使用教程|編輯:龔雪|2022-05-27 10:45:53.373|閱讀 245 次
概述:本文主要介紹如何在Winform開(kāi)發(fā)框架中下拉列表綁定字典以及使用緩存提高界面顯示速度,歡迎下載推薦產(chǎn)品體驗(yàn)!
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷售中 >>
相關(guān)鏈接:
在我們開(kāi)發(fā)WinForm界面的時(shí)候,往往需要綁定數(shù)據(jù)字典操作,也就是綁定一些下拉列表或者一些列表顯示等,以便我們方便選擇數(shù)據(jù)操作,常見(jiàn)的字典綁定操作就是對(duì)下拉列表的處理,本文是基于DevExpress界面的一些處理操作,原理也適用于常規(guī)Winform界面或者DotNetBar控件界面處理。另外對(duì)于緩存的處理,一般在基于單機(jī)版數(shù)據(jù)或者局域網(wǎng)API接口處理的字典綁定,速度是比較快的,基本上可以不用考慮緩存的處理,但是對(duì)于基于互聯(lián)網(wǎng)API接口的數(shù)據(jù)處理,往往受限于帶寬等原因,請(qǐng)求數(shù)據(jù)的速度沒(méi)有那么快,那么需要做好數(shù)據(jù)緩存處理,才可能更好的提高用戶體驗(yàn)。
對(duì)于普通的下拉列表控件,我們綁定操作就是先獲取字典數(shù)據(jù)列表,然后對(duì)它的數(shù)據(jù)項(xiàng)進(jìn)行添加操作即可,為了方便,我們往往做成一個(gè)擴(kuò)展函數(shù)的方式來(lái)進(jìn)行處理,并把這些通用的擴(kuò)展函數(shù)放到界面基類庫(kù)里面方便重用,這樣我們可以在設(shè)計(jì)到界面的數(shù)據(jù)綁定的時(shí)候,非常方便的調(diào)用了。
如類似下面的擴(kuò)展函數(shù)定義。
/// <summary> /// 綁定下拉列表控件為指定的數(shù)據(jù)字典列表 /// </summary> /// <param name="combo">下拉列表控件</param> /// <param name="itemList">數(shù)據(jù)字典列表</param> /// <param name="defaultValue">控件默認(rèn)值</param> /// <param name="emptyFlag">是否加入空值選項(xiàng)</param> public static void BindDictItems(this ComboBoxEdit combo, List<string> itemList, string defaultValue, bool emptyFlag = true) { combo.Properties.BeginUpdate();//可以加快 combo.Properties.Items.Clear(); combo.Properties.Items.AddRange(itemList); if (emptyFlag) { combo.Properties.Items.Insert(0, ""); } if (itemList.Count > 0) { combo.SetDropDownValue(defaultValue); } combo.Properties.EndUpdate();//可以加快 }
還有有時(shí)候我們需要一個(gè)顯示文本、值的鍵值對(duì)來(lái)對(duì)字典進(jìn)行處理,如定義的CListItem對(duì)象:
/// <summary> /// 框架用來(lái)記錄字典鍵值的類,用于Comobox等控件對(duì)象的值傳遞 /// </summary> [Serializable] public class CListItem { /// <summary> /// 顯示內(nèi)容 /// </summary> public string Text { get; set; } /// <summary> /// 實(shí)際值內(nèi)容 /// </summary> public string Value { get; set; } }
這樣我們綁定列表的擴(kuò)展函數(shù)在定義一個(gè)函數(shù),如下所示。
/// <summary> /// 綁定下拉列表控件為指定的數(shù)據(jù)字典列表 /// </summary> /// <param name="combo">下拉列表控件</param> /// <param name="itemList">數(shù)據(jù)字典列表</param> /// <param name="defaultValue">控件默認(rèn)值</param> /// <param name="emptyFlag">是否加入空值選項(xiàng)</param> public static void BindDictItems(this ComboBoxEdit combo, List<CListItem> itemList, string defaultValue, bool emptyFlag = true) { combo.Properties.BeginUpdate();//可以加快 combo.Properties.Items.Clear(); combo.Properties.Items.AddRange(itemList); if (emptyFlag) { combo.Properties.Items.Insert(0, new CListItem("")); } if (itemList.Count > 0) { if (!string.IsNullOrEmpty(defaultValue)) { combo.SetComboBoxItem(defaultValue); } else { combo.SelectedIndex = 0; } } combo.Properties.EndUpdate();//可以加快 }
當(dāng)然,除了上面的這兩個(gè)處理,我們還可以定義很多不同類型的重載方法,以便更方便處理相關(guān)的控件的字典數(shù)據(jù)綁定。
對(duì)于固定數(shù)據(jù)源List<string>、或者List<CListItem>來(lái)說(shuō),我們綁定的操作就非常簡(jiǎn)單。
List<CListItem> itemList= new List<CListItem>() { new CListItem("有"), new CListItem("無(wú)") }; txtItem.BindDictItems(itemList);
然后獲取對(duì)應(yīng)字典值的方式,我們可以定義一個(gè)擴(kuò)展函數(shù)來(lái)處理,如下代碼所示。
/// <summary> /// 獲取下拉列表的值 /// </summary> /// <param name="combo">下拉列表</param> /// <returns></returns> public static string GetComboBoxValue(this ComboBoxEdit combo) { CListItem item = combo.SelectedItem as CListItem; if (item != null) { return item.Value; } else { return ""; } }
對(duì)于以上的操作,我們這里還沒(méi)有涉及到字典模塊里面的數(shù)據(jù)源,只是提供一些常規(guī)的固定列表,我們知道,大多數(shù)的數(shù)據(jù)字典我們是通過(guò)字典模塊來(lái)進(jìn)行維護(hù)的。
因此我們也需要?jiǎng)討B(tài)的從字典庫(kù)上獲取對(duì)應(yīng)的字典集合來(lái)進(jìn)行綁定。字典的數(shù)據(jù),我們可以通過(guò)字典類型或者字典代碼來(lái)獲取,如下是通過(guò)字典類型獲取對(duì)應(yīng)的字典列表代碼。
BLLFactory<DictData>.Instance.GetDictByDictType(dictTypeName);
有了這些數(shù)據(jù)的獲取方法,我們就可以通過(guò)擴(kuò)展函數(shù)來(lái)進(jìn)一步擴(kuò)展我們綁定字典類別的方式了,如下擴(kuò)展函數(shù)所示。
/// <summary> /// 綁定下拉列表控件為指定的數(shù)據(jù)字典列表 /// </summary> /// <param name="combo">下拉列表控件</param> /// <param name="dictTypeName">數(shù)據(jù)字典類型名稱</param> /// <param name="defaultValue">控件默認(rèn)值</param> public static void BindDictItems(this ComboBoxEdit combo, string dictTypeName, string defaultValue) { Dictionary<string, string> dict = BLLFactory<DictData>.Instance.GetDictByDictType(dictTypeName); List<CListItem> itemList = new List<CListItem>(); foreach (string key in dict.Keys) { itemList.Add(new CListItem(key, dict[key])); } BindDictItems(combo, itemList, defaultValue); }
使用的時(shí)候,就非常簡(jiǎn)單了,如下代碼是實(shí)際項(xiàng)目中對(duì)字典列表綁定的操作,字典數(shù)據(jù)在字典模塊里面統(tǒng)一定義的。
/// <summary> /// 初始化數(shù)據(jù)字典 /// </summary> private void InitDictItem() { txtInDiagnosis.BindDictItems("入院診斷"); txtLeaveDiagnosis.BindDictItems("最后診斷"); //初始化代碼 this.txtFollowType.BindDictItems("隨訪方式"); this.txtFollowStatus.BindDictItems("隨訪狀態(tài)"); }
這樣就非常簡(jiǎn)化了我們對(duì)字典數(shù)據(jù)源的綁定操作了,非常方便易讀,下面是其中一個(gè)功能界面的下拉列表展示。
DevExpress WinForm擁有180+組件和UI庫(kù),能為Windows Forms平臺(tái)創(chuàng)建具有影響力的業(yè)務(wù)解決方案。DevExpress WinForms能完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序,無(wú)論是Office風(fēng)格的界面,還是分析處理大批量的業(yè)務(wù)數(shù)據(jù),它都能輕松勝任!
本文轉(zhuǎn)載自:
DevExpress技術(shù)交流群6:600715373 歡迎一起進(jìn)群討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自: