一、所有編輯器的公共功能
- 全部都可以綁定數據;
- 全部都可以獨立使用或用於由 Developer Express 提供的容器控件 (XtraGrid、XtraVerticalGrid、XtraTreeList 和 XtraBars) 內的內置編輯;
- 全部都使用相同的樣式、外觀與感覺、以及工具提示機制,因為它們都派生於 BaseEdit 類。
要把某個特定的編輯器嵌入到容器控件中,則必須創建一個相應的 Repository 項。 Repository 項是一個組件,存儲了屬於某個對應編輯器的屬性和事件。 容器控件使用由 repository 項提供的信息,來創建所需的全功能編輯器。
二、編輯器列表
三、控件的演示代碼:
四、編輯器的層次結構
- BaseEdit 的直屬子類 (不包括 TextEdit 編輯器)。
該組中的每個編輯器都有自己的功能,並且沒有與其他編輯器相同的功能。 - TextEdit 類及其直屬子 MemoEdit 編輯器。
這些編輯器分別顯示一個支持單行或多行文本編輯的文本框。 請參閱 文本編輯概述 主題,來獲得關於這些編輯器及其子類的特定功能的細節。 - ButtonEdit 類及其直屬子 HyperLinkEdit 編輯器。
除了繼承的功能之外,這些編輯器還可以顯示編輯器按鈕。 請參閱 編輯器按鈕概述 和 編輯器按鈕快捷鍵 主題,來獲得關於定制編輯器按鈕的其他信息。 - BaseSpinEdit 類的直屬子類 —— SpinEdit 和 TimeEdit 控件。
除了繼承的功能之外,這些編輯器還允許最終用戶使用微調按鈕來修改取值。 請參閱 微調按鈕 主題,來獲得其他信息。 - PopupBaseEdit 類的子類。
除了繼承的功能之外,這些編輯器還可以顯示彈出窗口,並且允許控制其可用性、可視性和彈出窗口功能的其他外觀。 請參閱 下拉編輯器概述 主題來學習更多內容。 - BlobBaseEdit 類的子類 —— ImageEdit 和 MemoExEdit 控件。
除了繼承的功能之外,這些編輯器還提供了使用其彈出窗口編輯 BLOB 數據的功能。 請參閱 BLOB 編輯器 主題,來獲得其他信息。 - LookUpEditBase 類的子類實現了查找功能。 該類有兩個子類: LookUpEdit 和 GridLookUpEdit 編輯器。
GridLookUpEdit 控件使用一個內嵌 XtraGrid 控件實現了查找功能。 此控件由 XtraGrid 套件提供,並且不包括在 XtraEditors 庫中。
要獲得更多關於查找功能的信息,請參閱 LookUpEdit 主題。 - ComboBoxEdit 類及其子類 (ImageComboBoxEdit、MRUEdit 和 FontEdit)。
這些編輯器允許最終用戶從下拉列表中選取某項取值。 請參閱 組合框編輯器 主題,來獲知細節。 - TrackBarControl 類及其子類 (RangeTrackBarControl 和 ZoomTrackBarControl)
呈現滑動欄 —— 顯示一個或兩個滑動塊的控件,允許最終用戶選取某個取值或某個取值范圍。 - ProgressBarBaseControl 的子類 —— ProgressBarControl 和 MarqueeProgressBarControl。
這些控件適用於指示漫長操作的進度。
BaseEdit
BaseCheckEdit
PictureEdit
ProgressBarBaseControl
RadioGroup
RatingControl
SparklineEdit
TextEdit
TokenEdit
TrackBarControl
TextEdit
MemoEdit
ButtonEdit
HyperLinkEdit
PopupBaseEdit
BaseSpinEdit
SpinEdit
TimeEdit
BlobBaseEdit
ImageEdit
MemoExEdit
CalcEdit
ColorEdit
DateEdit
PopupBaseAutoSearchEdit
ComboBoxEdit
LookUpEditBase
LookUpEdit
TreeListLookUpEdit
GridLookUpEditBase
GridLookUpEdit
SearchLookUpEdit
PopupContainerEdit
CheckedComboBoxEdit
ResourcesPopupCheckedListBoxControl
PopupGalleryEdit
五、與.net基本的控件比較
1. 命名空間(NameSpace)
.net基本控件的類都在System.Windows.Forms的命名空間下,DevExpress的控件類在DevExpress命名空間下
2. 可以代替.net的控件
DevExpress的大部分控件都已可以代替.net的基本控件。如:
- 按鈕:
System.Windows.Forms.Button -> DevExpress.XtraEditors.SimpleButton - 文本框:
System.Windows.Forms.TextBox -> DevExpress.XtraEditors.TextEdit - 復選框
System.Windows.Forms.CheckBox -> DevExpress.XtraEditors.CheckEdit - 下拉框:
System.Windows.Forms.ComboBox -> DevExpress.XtraEditors.ComboBoxEdit - 日 期:
System.Windows.Forms.DateTimePicker -> DevExpress.XtraEditors.DateEdit / DevExpress.XtraEditors.TimeEdit
這里就不一一列舉了,認真看看,相信一定找出很多可以替代的控件
六、幾個比較重要、常用的屬性
1:EditValue
DevExpress.XtraEditors.*Edit 的控件都不可少的一個EditValue屬性。
如:DevExpress.XtraEditors.*Edit
通常,EditValue會跟Text的值是一樣的。
只是EditValue的類型為Object,
而Text的屬性為String,
也就是EditValue通常可以代替Text屬性。
2. Enable 和 Visable
是否禁用和是否可見
3. Properties
設置控件一些特征
例如 是否只讀:
textEdit.Properties.ReadOnly = true;
不允許獲得焦點
textEdit.Properties.AllowFocused = false;
禁止空值輸入
textEdit.Properties.AllowNullInput = false;
// 當這個屬性應用在TimeEdit中,它的清除按鈕,將會禁用(灰掉)
禁止編輯器輸入
comboBoxEdit.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;// 只選模式,不能輸入文本
4. Appearance 設置風格。
Dexpress把所有設置控件風格的特性都放到Appearance屬性下。
例:
//前景色
simpleButton.Appearance.ForeColor = Color.Red;
//背景色
simpleButton.Appearance.BackColor = Color.Red;
Appearance.TextOptions 文本對齊操作
// 居中對齊
simpleButton.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
// 自動換行。當控件的寬度容不下文本的長度,會自動換行。
simpleButton.Appearance.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
注意:
在某些控件中, Properties屬性下才有Apperarance
DevExpress.XtraEditors.TextEdit textEdit = …;
textEdit.Properties.Appearance.ForeColor = Color.Red;
七. 幾個常用的控件
1:LookUpEdit
用強大的LookUpEdit代替ComboBox
- ComboBox不支持數據綁定
- 由於DevExpress的ComboBox天生存在數據綁定缺陷,
所以有時我們要做數據綁定這種殺雞小事時,不得不使用牛刀LooUpEdit。
如下代碼,可用實現一個ComboBox:
// 禁止文本輸入 this.lookUpEdit1.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor; // 默認為null的顯示 this.lookUpEdit1.Properties.NullText = "[請選擇類別]"; // 加入一個顯示列 this.lookUpEdit1.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Name")); // 不顯示頁眉(包括列頭) this.lookUpEdit1.Properties.ShowHeader = false; // 不顯示頁腳(包括關閉按鈕) this.lookUpEdit1.Properties.ShowFooter = false; // 要顯示的字段,Text獲得 this.lookUpEdit1.Properties.DisplayMember = "Name"; // 實際值的字段,EditValue獲得 this.lookUpEdit1.Properties.ValueMember = "Value"; // 數據綁定 ICollection list = Global.ClassCategoryList; // 綁定數據 this.lookUpEdit1.Properties.DataSource = list; // 設置行數(根據這個自動設置高度) this.lookUpEdit1.Properties.DropDownRows = list.Count;
2. GridControl
GridControl可以代替.net的System.Windows.Forms.DataGrid控件。
GirdControl只是一個容器控件,必須要求GridView視圖作為它的子控件。
GridControl可以包含多個視圖,可以實現視圖的切換。
每個GridView視圖必須包含列(Column)
GridControl支持層級視圖
GridControl 經常設置的屬性
使用導航器
this.gridControl1.UseEmbeddedNavigator = true; this.gridControl1.EmbeddedNavigator.Buttons.Append.Visible = false; this.gridControl1.EmbeddedNavigator.Buttons.CancelEdit.Visible = false; this.gridControl1.EmbeddedNavigator.Buttons.Edit.Visible = false; this.gridControl1.EmbeddedNavigator.Buttons.EndEdit.Visible = false; this.gridControl1.EmbeddedNavigator.Buttons.Remove.Visible = false; this.gridControl1.EmbeddedNavigator.Buttons.First.Visible = true; this.gridControl1.EmbeddedNavigator.Buttons.Last.Visible = true; this.gridControl1.EmbeddedNavigator.Buttons.Next.Visible = true; this.gridControl1.EmbeddedNavigator.Buttons.NextPage.Visible = true; this.gridControl1.EmbeddedNavigator.Buttons.Prev.Visible = true; this.gridControl1.EmbeddedNavigator.Buttons.PrevPage.Visible = true;
GridView 經常設置的屬性
// 禁止編輯 this.gridView1.OptionsBehavior.Editable = false; // 不允許使用過濾 this.gridView1.OptionsCustomization.AllowFilter = false; // 不允許使用排序 this.gridView1.OptionsCustomization.AllowSort = false; // 不顯示組面板 this.gridView1.OptionsView.ShowGroupPanel = false; // 如果寬度溢出,自動出現滾動條 this.gridView1.OptionsView.ColumnAutoWidth =true; // 禁止單元格獲得焦點 this.gridView1.OptionsSelection.EnableAppearanceFocusedCell = false; // 選擇的行背景透明 this.gridView1.Appearance.SelectedRow.BackColor = Color.Transparent;
事件
//訂閱行焦點改變事件 this.gridView1.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(this.gridView1_FocusedRowChanged); //驗證編輯器(單元格)值輸入 this.gridView1.ValidatingEditor += new DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventHandler(this.gridView1_ValidatingEditor); private void gridView1_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e) { if (gridView1.FocusedColumn == col產品名稱) { if (string.IsNullOrEmpty(e.Value as string)) { e.ErrorText = "產品名稱不能為空"; e.Valid = false; } } }
//訂閱設置行風格事件 this.gridView1.RowStyle += new DevExpress.XtraGrid.Views.Grid.RowStyleEventHandler(this.gridView1_RowStyle); private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e) { object value = gridView1.GetRowCellValue(e.RowHandle, "中止"); if (value!=null&&(bool)value) { e.Appearance.ForeColor = Color.Red; } } this.gridView1.CustomColumnDisplayText += new DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventHandler(this.gridView1_CustomColumnDisplayText); private DataTable _CategoryList; public DataTable CategoryList { get { if (_CategoryList == null) { _CategoryList=GetData("select * from 產品類別"); DataColumn pk = _CategoryList.Columns["類別ID"]; _CategoryList.PrimaryKey = new DataColumn[] {pk}; } return _CategoryList; } } private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e) { if (e.Column.Name == "col類別ID") { e.DisplayText = CategoryList.Rows.Find(e.Value)["類別名稱"] as string; } } // 行單元格對齊 this.gridView1.RowCellDefaultAlignment += new DevExpress.XtraGrid.Views.Base.RowCellAlignmentEventHandler(this.gridView1_RowCellDefaultAlignment); private void gridView1_RowCellDefaultAlignment(object sender, DevExpress.XtraGrid.Views.Base.RowCellAlignmentEventArgs e) { e.HorzAlignment = DevExpress.Utils.HorzAlignment.Near; }
3. 界面操作
3.1 根據條件操作行或列風格
3.2 添加RepositoryItem(內嵌元素In-place EditorRepository)
3.3 列匯總
首先,設置this.gridView1.OptionsView.ShowFooter = true;
this.col庫存量.SummaryItem.DisplayFormat = “總量:{0}”;
// 六種:Sum,Average,Count,Max,Min,Custom
col單位數量.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;