小Q的博客

  • 首页
  • net编程
    • 产品和框架
    • 开发实例
    • 经验技巧
    • 开源组件
  • wp独立站
  • 自媒体
  • 日记本
  • 工具箱
每个程序员,都应该有一个自己的博客站
  1. 首页
  2. net编程
  3. 开源组件
  4. 正文

devexpress控件教程 能加载任何控件的下拉菜单

2023年1月3日 285点热度 0人点赞 0条评论

DevExpress控件很强大,今天开始写一些关于这个控件的博客,希望能对小伙伴们有所帮助。今天的内容是打造一个万能的下拉菜单控件。一般来说,ComboBoxEdit控件已经够用了,加载编码+名称的数据。但有的项目要求树控件展示、Grid控件展示,或者自定义控件展示数据。因为看到名称的时候,还要看到规则、颜色、尺寸啥的。今天就来实现3种下拉菜单

目录

  • 控件1:ComboTree
  • 控件2:ComboGrid
  • 控件3:ComboAnyControl

控件1:ComboTree

这个控件是自定义开发的,大约1000行代码。实现的是下拉加载Tree控件,同时支持单选和多选。

comboTree控件示例

示例代码如下

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控件即可。

comboGrid控件示例

调用示例代码

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;
}

 

完整源码下载

 

相关阅读

devexpress中grid控件教程 多线程异步加载数据,进度条展示

devexpress控件教程 零代码打造展示数据的工作台

标签: combogrid combotree devexpress控件 devexpress教程 net教程 下拉菜单
最后更新:2023年1月3日

小Q

80后中年不油腻大叔,喜欢编写代码、打羽毛球、做木制玩具。目前定居浙江杭州

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2022 小Q的博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

浙ICP备2022019157号-2