DevExpress控件很强大,今天开始写一些关于这个控件的博客,希望能对小伙伴们有所帮助。今天的内容是打造一个万能的下拉菜单控件。一般来说,ComboBoxEdit控件已经够用了,加载编码+名称的数据。但有的项目要求树控件展示、Grid控件展示,或者自定义控件展示数据。因为看到名称的时候,还要看到规则、颜色、尺寸啥的。今天就来实现3种下拉菜单
控件1:ComboTree
这个控件是自定义开发的,大约1000行代码。实现的是下拉加载Tree控件,同时支持单选和多选。
示例代码如下
public List<treeviewNodeModel> GetListTestData() { List<treeviewNodeModel> list1 = new List<treeviewNodeModel>(); list1.Add(new treeviewNodeModel() { Value = "node1", Text = "结点1", ParentValue = BaseHelper.RootValue, }); list1.Add(new treeviewNodeModel() { Value = "node11", Text = "结点11", ParentValue = "node1", }); list1.Add(new treeviewNodeModel() { Value = "node12", Text = "结点12", ParentValue = "node1", }); list1.Add(new treeviewNodeModel() { Value = "node2", Text = "结点2", ParentValue = BaseHelper.RootValue, }); list1.Add(new treeviewNodeModel() { Value = "node21", Text = "结点21", ParentValue = "node2", }); list1.Add(new treeviewNodeModel() { Value = "node22", Text = "结点22", ParentValue = "node2", }); return list1; } private void btnLoadComboTree_Click(object sender, EventArgs e) { List<treeviewNodeModel> list1 = this.GetListTestData(); this.fmComboTree.SetTreeViewDataSource(list1, "结点列表", false); }
控件2:ComboGrid
这里用到的是DevExpress的原生控件GoodsLookupEdit,不需要自定义开发,直接加载好Grid控件即可。
调用示例代码
private void btnLoadComboGrid_Click(object sender, EventArgs e) { List<treeviewNodeModel> list1 = this.GetListTestData(); this.fmComboGrid.Properties.DataSource = list1; this.fmComboGrid.Properties.ValueMember = "Value"; this.fmComboGrid.Properties.DisplayMember = "Text"; //设置Grid显示的列信息 List<string> listCol = new List<string>() { "Value,编码", "Text,名称", "ParentValue,父编码", "TagModel,Tag对象" }; foreach (string col in listCol) { string[] array1 = col.Split(','); GridColumn gcAdd = new GridColumn(); gcAdd.FieldName = array1[0]; gcAdd.Caption = array1[1]; gcAdd.Width = 100; gcAdd.Fixed = FixedStyle.Left; gcAdd.Visible = true; this.fmComboGrid.Properties.View.Columns.Add(gcAdd); } //其他属性设置 this.fmComboGrid.Properties.ImmediatePopup = true; this.fmComboGrid.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard; this.fmComboGrid.Properties.PopupWidthMode = DevExpress.XtraEditors.PopupWidthMode.ContentWidth; //设定列表的序号的宽度和显示文本 this.fmComboGrid.Properties.View.IndicatorWidth = 40; this.fmComboGrid.Properties.View.CustomDrawRowIndicator += (s1, e1) => { if (e1.Info.IsRowIndicator && e1.RowHandle >= 0) { e1.Info.DisplayText = (e1.RowHandle + 1).ToString(); } }; }
控件3:ComboAnyControl
这个控件可以下拉任意控件,我们用到了PopupContainerControl和PopupContainerEdit。简单说,在PopupContainerControl中设计好下拉要展示的内容,然后再挂到PopupContainerEdit控件即可。简单粗暴(但是我喜欢),这里我设计了一个ButtonEdit+TextEdit+GridControl的控件,支持全选。
调用控件示例
private void btnLoadComboAny_Click(object sender, EventArgs e) { List<GridColumnModel> gridColumns = new List<GridColumnModel>(); gridColumns.Add(new GridColumnModel() { ColumnID = "Value", ColumnParentID = "Root", ColumnText = "编码", Width = "70", EditType = GridColumnEditType.TextBox.ToString(), }); gridColumns.Add(new GridColumnModel() { ColumnID = "Text", ColumnParentID = "Root", ColumnText = "名称", Width = "90", EditType = GridColumnEditType.TextBox.ToString(), }); gridColumns.Add(new GridColumnModel() { ColumnID = "ParentValue", ColumnParentID = "Root", ColumnText = "父编码", Width = "80", EditType = GridColumnEditType.TextBox.ToString(), }); FormUIHelper.InitGridColumn(this.gridviewPopup1, gridColumns); GridOptionModel gridOption = new GridOptionModel() { ShowDetailTabs = false, ShowGroupPanel = false, ShowAutoFilterRow = true, AllowFilter = true, EditorShowMode = GridEditorShowMode.MouseUp, IndicatorWidth = 40, MultiSelect = true }; FormUIHelper.SetGridStyle(this.gridviewPopup1, GridStyle.List, gridOption); List<treeviewNodeModel> list1 = this.GetListTestData(); this.gridPopup1.DataSource = list1; this.fmSelValue.ButtonClick += fmSelValue_ButtonClick; this.btnSelectAll.Click += btnSelectAll_Click; this.fmComboAny.Properties.PopupControl = this.popupContainerControl1; this.popupContainerControl1.Leave += popupContainerControl1_Leave; this.gridviewPopup1.RowClick += gridviewPopup1_RowClick; this.gridviewPopup1.CustomDrawRowIndicator += gridviewPopup1_CustomDrawRowIndicator; }
相关阅读
文章评论