winform dev 网格树形下拉 控件封装

Dev 官网连接
TreeList
PopupContainerControl
PopupContainerEdit

效果图:
在这里插入图片描述

添加一个组件 开始自定义控件
在这里插入图片描述

文件结构
在这里插入图片描述

Entity Code

using System;

namespace Test
{
    public class DataDto
    {
        public Guid Id { get; set; }
        public Guid ParentId { get; set; }
        public string Name { get; set; }
        public string Value { get; set; }
    }
}

Event Code:

using System;
using System.Collections.Generic;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;

namespace Test
{
    public partial class ComponentName : PopupContainerControl
    {
    	// 控制清除 确定 取消 三个按钮的显示
        public bool showCancelButton = true;
        public bool showOKButton = false;
        public bool showClearButton = false;

        private bool firstLoad = true;

        private List<DataDto> ListData { get; set; }
        public DataDto Current { get; set; }

        public event Action<DataDto> ChangeValue;

        protected PopupContainerEdit edit;

        private RepositoryItemPopupContainerEdit PopupControl { get; set; }

        public ComponentName()
        {
            this.InitializeComponent();

            this.btnCancel.Visible = this.showCancelButton;
            this.btnClear.Visible = this.showClearButton;
            this.btnOK.Visible = this.showOKButton;

            this.ListData = new List<DataDto>();

            this.PopupControl = new RepositoryItemPopupContainerEdit
            {
                TextEditStyle = TextEditStyles.Standard,
                PopupControl = this
            };
            this.PopupControl.Popup += PopupControl_Popup;
            this.PopupContainerProperties.ShowPopupCloseButton = false;

            this.PopupContainerProperties.PopupWidthMode = PopupWidthMode.UseEditorWidth;

            this.PopupControl.BeforePopup += PopupControl_BeforePopup;
            this.PopupControl.Closed += PopupControl_Closed;
            this.btnClear.Click += BtnClear_Click;
            this.btnOK.Click += BtnOK_Click;
            this.btnCancel.Click += BtnCancel_Click;
            this.treeList.FocusedNodeChanged += TreeList_FocusedNodeChanged;
        }

        private void PopupControl_BeforePopup(object sender, EventArgs e)
        {
            if (firstLoad)
            {
                firstLoad = false;
                this.ListData = 数据源;
                this.treeList.DataSource = this.ListData;
                this.treeList.KeyFieldName = "Id";
                this.treeList.ParentFieldName = "ParentId";

                this.treeList.Columns[nameof(DataDto.Name)].Caption = "Name";
                this.treeList.Columns[nameof(DataDto.Value)].Visible = false;
            }
        }

        /// <summary>
        /// 在打开下拉弹窗时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PopupControl_Popup(object sender, EventArgs e)
        {
            if (sender is PopupContainerEdit edit)
            {
                this.edit = edit;
            }
        }

        /// <summary>
        /// 行焦点改变 取选中行的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TreeList_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            var node = this.treeList.GetFocusedRow() as DataDto;
        }

        /// <summary>
        /// 弹窗关闭时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PopupControl_Closed(object sender, ClosedEventArgs e)
        {
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.OwnerEdit?.ClosePopup();
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnOK_Click(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// 清除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnClear_Click(object sender, EventArgs e)
        {
            this.OwnerEdit?.ClosePopup();
            this.edit.EditValue = null;
        }
    }
}

Layout Code

namespace Test
{
    partial class ComponentName
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region 组件设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            this.panel = new System.Windows.Forms.Panel();
            this.treeList = new DevExpress.XtraTreeList.TreeList();
            this.btnOK = new DevExpress.XtraEditors.SimpleButton();
            this.btnCancel = new DevExpress.XtraEditors.SimpleButton();
            this.btnClear = new DevExpress.XtraEditors.SimpleButton();
            ((System.ComponentModel.ISupportInitialize)(this.treeList)).BeginInit();
            this.SuspendLayout();
            //
            // this popupContainer
            //
            this.Controls.Add(this.treeList);
            this.Controls.Add(this.panel);
            this.Location = new System.Drawing.Point(3, 6);
            this.Name = "popupContainer";
            this.Size = new System.Drawing.Size(484, 338);
            this.TabIndex = 0;
            // 
            // treeList
            // 
            this.treeList.DataSource = null;
            this.treeList.Dock = System.Windows.Forms.DockStyle.Fill;
            this.treeList.Location = new System.Drawing.Point(0, 0);
            this.treeList.Name = "treeList";
            this.treeList.Size = new System.Drawing.Size(484, 288);
            this.treeList.TabIndex = 1;
            this.treeList.OptionsBehavior.Editable = false;
            // 
            // panel
            // 
            this.panel.Controls.Add(this.btnClear);
            this.panel.Controls.Add(this.btnCancel);
            this.panel.Controls.Add(this.btnOK);

            this.panel.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel.Location = new System.Drawing.Point(0, 288);
            this.panel.Name = "panel";
            this.panel.Size = new System.Drawing.Size(484, 53);
            this.panel.TabIndex = 2;
            // 
            // btnClear
            // 
            this.btnClear.Location = new System.Drawing.Point(12, 13);
            this.btnClear.Name = "btnClear";
            this.btnClear.Size = new System.Drawing.Size(75, 23);
            this.btnClear.TabIndex = 5;
            this.btnClear.Text = "清除";
            // 
            // btnCancel
            // 
            this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnCancel.Location = new System.Drawing.Point(397, 13);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(75, 23);
            this.btnCancel.TabIndex = 4;
            this.btnCancel.Text = "取消";
            // 
            // btnOK
            // 
            this.btnOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnOK.Location = new System.Drawing.Point(287, 13);
            this.btnOK.Name = "btnOK";
            this.btnOK.Size = new System.Drawing.Size(75, 23);
            this.btnOK.TabIndex = 3;
            this.btnOK.Text = "确定";

            this.ResumeLayout();
            ((System.ComponentModel.ISupportInitialize)(this.treeList)).EndInit();
        }

        private System.Windows.Forms.Panel panel;
        private DevExpress.XtraEditors.SimpleButton btnClear;
        private DevExpress.XtraEditors.SimpleButton btnCancel;
        private DevExpress.XtraEditors.SimpleButton btnOK;
        private DevExpress.XtraTreeList.TreeList treeList;
        #endregion
    }
}

From中使用

using System;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private ComponentName treeListGridLookUpEdit;

        private void Form1_Load(object sender, EventArgs e)
        {
            this.treeListGridLookUpEdit = new ComponentName();
            this.treeListGridLookUpEdit.ChangeValue += TreeListGridLookUpEdit_ChangeValue;

			// 绑定数据源之后 将控件绑定到网格
            this.gridCtr.DataSource = 数据源;
            this.gridView.Columns[nameof(DataDto.Name)].ColumnEdit = this.treeListGridLookUpEdit.PopupContainerProperties;
        }

        private void TreeListGridLookUpEdit_ChangeValue(DataDto obj)
        {
            // obj 当前选中的值
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值