轉帖|使用教程|編輯:龔雪|2022-09-16 10:05:46.037|閱讀 179 次
概述:本文將主要為大家介紹如何在WinForm應用程序的時如何使用DevExpress日程控件開發(fā)相關日程視圖等,有相關實用組件推薦使用,按需下載~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
在一些應用場景中,我們可能需要記錄某一天,某個時段的日程安排,那么這個時候就需要引入了DevExpress的日程控件XtraScheduler了,這個控件功能非常強大,提供了很好的界面展現(xiàn)方式,以及很多的事件、屬性給我們定制修改,能很好滿足我們的日程計劃安排的需求。在上文中,我們?yōu)榇蠹医榻B了DevExpress日程控件(XtraScheduler)的一些表現(xiàn)效果和基本使用(點擊這里回顧>>),本文將繼續(xù)講解日程控件XtraScheduler的數(shù)據(jù)綁定。
DevExpress WinForms能完美構建流暢、美觀且易于使用的應用程序,無論是Office風格的界面,還是分析處理大批量的業(yè)務數(shù)據(jù),它都能輕松勝任!
在日程控件里面,我們最重要,最關注的莫過于它的數(shù)據(jù)綁定及內容顯示了,因為只有這樣,我們才可以用于實價的應用當中,為用戶顯示他所需的數(shù)據(jù),并存儲我們所需要的數(shù)據(jù)。
在日程控件里面,有相應的引導我們進行這樣的處理,還是非常不錯的。
數(shù)據(jù)的綁定,我們需要了解日程控件的默認處理方式,因為它也提供了一些數(shù)據(jù)字段的信息,我們從控件的對象里面,看到有創(chuàng)建數(shù)據(jù)庫的信息,里面有一些表的字段,我們可以參考來創(chuàng)建我們的數(shù)據(jù)存儲信息,其中就包括了資源Resource的存儲,日程事件安排Appointments的存儲,如下所示。
根據(jù)這個里面的字段信息,我們可以建立自己的數(shù)據(jù)庫模型如下所示。
在數(shù)據(jù)庫里面創(chuàng)建這兩個表,并根據(jù)這些表對象,使用代碼生成工具Database2Sharp進行代碼的快速生成,然后復制生成的代碼到具體的測試項目里面,生成的代碼無需任何修改即可直接使用在具體項目里面,測試項目如下代碼結構所示。
如日程資源對象的數(shù)據(jù)庫信息,就會轉換為具體的實體類信息,供我們在界面中使用了,這樣也符合我的Winform開發(fā)框架的實體類綁定規(guī)則,提高我們數(shù)據(jù)的強類型約束。
如資源對象的實體類代碼生成如下所示。
/// <summary> /// 日程資源 /// </summary> [DataContract] public class AppResourceInfo : BaseEntity { /// <summary> /// 默認構造函數(shù)(需要初始化屬性的在此處理) /// </summary> public AppResourceInfo() { this.ID = 0; this.ResourceId = 0; this.Color = 0; this.Image = new byte[] { }; } #region Property Members [DataMember] public virtual int ID { get; set; } /// <summary> /// 資源ID /// </summary> [DataMember] public virtual int ResourceId { get; set; } /// <summary> /// 資源名稱 /// </summary> [DataMember] public virtual string ResourceName { get; set; } /// <summary> /// 顏色 /// </summary> [DataMember] public virtual int Color { get; set; } /// <summary> /// 圖形 /// </summary> [DataMember] public virtual byte[] Image { get; set; } /// <summary> /// 自定義 /// </summary> [DataMember] public virtual string CustomField1 { get; set; } #endregion }
有了這些對象,我們還需要做的就是綁定控件和保存控件數(shù)據(jù)到數(shù)據(jù)庫里面的處理。
但是這里還需要注意一個問題就是,這個日程控件數(shù)據(jù)是通過字段映射的方式進行數(shù)據(jù)綁定的,也就是它本身也提供了幾個常規(guī)字段的信息,因此我們需要把它們的屬性和數(shù)據(jù)庫的字段(這里是實體類)的信息進行匹配。
如我們可以通過綁定如下,事項Appointments和Resources的Mappings處理。
/// <summary> /// 設置日程控件的字段映射 /// </summary> /// <param name="control">日程控件</param> private void SetMappings(SchedulerControl control) { AppointmentMappingInfo appoint = control.Storage.Appointments.Mappings; appoint.AllDay = "AllDay"; appoint.Description = "Description"; appoint.End = "EndDate"; appoint.Label = "AppLabel"; appoint.Location = "Location"; appoint.RecurrenceInfo = "RecurrenceInfo"; appoint.ReminderInfo = "ReminderInfo"; appoint.ResourceId = "ResourceId"; appoint.Start = "StartDate"; appoint.Status = "Status"; appoint.Subject = "Subject"; appoint.Type = "EventType"; ResourceMappingInfo res = control.Storage.Resources.Mappings; res.Caption = "ResourceName"; res.Color = "Color"; res.Id = "ResourceId"; res.Image = "Image"; }
確定控件屬性和實體類之間關系后,我們就需要從數(shù)據(jù)庫里面加載信息了。我們在窗體的代碼里面增加兩個資源對象的集合列表,如下代碼所示。
//日程資源集合和事件列表 private List<AppResourceInfo> ResourceList = new List<AppResourceInfo>(); private List<UserAppointmentInfo> EventList = new List<UserAppointmentInfo>();
然后就是把數(shù)據(jù)從數(shù)據(jù)庫里面,通過開發(fā)框架底層的工廠類進行數(shù)據(jù)的提取,如下代碼所示。
private void btnLoadData_Click(object sender, EventArgs e) { //從數(shù)據(jù)庫加載日程信息 List<AppResourceInfo> resouceList = BLLFactory<AppResource>.Instance.GetAll(); this.schedulerStorage1.Resources.DataSource = resouceList; List<UserAppointmentInfo> eventList = BLLFactory<UserAppointment>.Instance.GetAll(); this.schedulerStorage1.Appointments.DataSource = eventList; if (resouceList.Count > 0) { MessageDxUtil.ShowTips("數(shù)據(jù)加載成功"); } else { MessageDxUtil.ShowTips("數(shù)據(jù)庫不存在記錄"); } }
而保存數(shù)據(jù),我們把對象里面的集合存儲到數(shù)據(jù)庫里面即可。
private void btnSave_Click(object sender, EventArgs e) { int count = BLLFactory<AppResource>.Instance.GetRecordCount(); if (count == 0) { try { foreach (AppResourceInfo info in ResourceList) { BLLFactory<AppResource>.Instance.Insert(info); } foreach (UserAppointmentInfo info in EventList) { BLLFactory<UserAppointment>.Instance.Insert(info); } MessageDxUtil.ShowTips("數(shù)據(jù)保存成功"); } catch (Exception ex) { LogTextHelper.Error(ex); MessageDxUtil.ShowError(ex.Message); } } else { MessageDxUtil.ShowTips("數(shù)據(jù)庫已存在數(shù)據(jù)"); } }
這樣,通過代碼工具Database2Sharp生成的代碼,直接具有數(shù)據(jù)存儲和獲取的功能,例子就很容易明白和處理了,在實際的項目中,我們可能還需要存儲用戶的額外信息,如公司、部門、自定義信息等等,當然也可以通過這樣的模式進行快速的開發(fā),從而實現(xiàn)高效、統(tǒng)一、穩(wěn)定的系統(tǒng)開發(fā)過程。
但是,言歸正傳,我們前面介紹的字段,都是控件里面有的內容,如果是控件里面沒有,我們需要增加的自定義屬性,那么我們應該如何處理呢,還有默認的日程界面可以修改嗎,等等這些也是我們經(jīng)常會碰到的問題。
首先我們在日程控件界面上,通過連接按鈕的方式,創(chuàng)建一個自定義的日程窗體,如下所示:
這樣我們就可以看到,在項目里面增加了一個日程編輯框了,打開窗體界面,并增加一個自定義的控件內容,最終界面如下所示。
默認的后臺代碼里面,具有了LoadFormData和SaveFormData兩個重載的方法,這里就是留給我們對自定義屬性進行處理的方法體了。
我們在其中增加部分自定義屬性字段的映射處理即可,如下代碼所示。
/// <summary> /// Add your code to obtain a custom field value and fill the editor with data. /// </summary> public override void LoadFormData(DevExpress.XtraScheduler.Appointment appointment) { //加載自定義屬性 txtCustom.Text = (appointment.CustomFields["CustomField1"] == null) ? "" : appointment.CustomFields["CustomField1"].ToString(); base.LoadFormData(appointment); } /// <summary> /// Add your code to retrieve a value from the editor and set the custom appointment field. /// </summary> public override bool SaveFormData(DevExpress.XtraScheduler.Appointment appointment) { //保存自定義屬性 appointment.CustomFields["CustomField1"] = txtCustom.Text; return base.SaveFormData(appointment); }
然后我們記得在主體窗體的映射里面,為他們增加對應的字段映射即可,映射代碼如下所示。
AppointmentCustomFieldMappingCollection appointCust = control.Storage.Appointments.CustomFieldMappings; appointCust.Add(new AppointmentCustomFieldMapping("CustomField1","CustomField1"));
這樣就構成了一個完整的映射信息。
/// <summary> /// 設置日程控件的字段映射 /// </summary> /// <param name="control">日程控件</param> private void SetMappings(SchedulerControl control) { AppointmentMappingInfo appoint = control.Storage.Appointments.Mappings; appoint.AllDay = "AllDay"; appoint.Description = "Description"; appoint.End = "EndDate"; appoint.Label = "AppLabel"; appoint.Location = "Location"; appoint.RecurrenceInfo = "RecurrenceInfo"; appoint.ReminderInfo = "ReminderInfo"; appoint.ResourceId = "ResourceId"; appoint.Start = "StartDate"; appoint.Status = "Status"; appoint.Subject = "Subject"; appoint.Type = "EventType"; AppointmentCustomFieldMappingCollection appointCust = control.Storage.Appointments.CustomFieldMappings; appointCust.Add(new AppointmentCustomFieldMapping("CustomField1","CustomField1")); ResourceMappingInfo res = control.Storage.Resources.Mappings; res.Caption = "ResourceName"; res.Color = "Color"; res.Id = "ResourceId"; res.Image = "Image"; }
以上就是我在整合日程控件XtraScheduler的經(jīng)驗總結,其中已經(jīng)考慮了數(shù)據(jù)存儲和顯示,以及快速開發(fā)的幾個方面,當然我們可以根據(jù)這些案例,做出更好的日程應用來了。
本文轉載自:
DevExpress技術交流群6:600715373 歡迎一起進群討論
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自: