dev TreeList 转https://blog.csdn.net/weixin_44734310/article/details/117929390
this.treeList.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder; // 获取主题的颜色 但是感觉没啥用 var skin = CommonSkins.GetSkin(DevExpress.LookAndFeel.UserLookAndFeel.Default); Color controlColor = skin.Colors.GetColor("Control"); this.treeList.Appearance.Empty.BackColor = controlColor; this.treeList.Appearance.Row.BackColor = controlColor; this.treeList.Appearance.FocusedRow.BackColor = controlColor; this.treeList.Appearance.FocusedCell.BackColor = controlColor; this.treeList.OptionsSelection.EnableAppearanceFocusedCell = false; this.treeList.OptionsSelection.EnableAppearanceFocusedRow = false; this.treeList.VertScrollVisibility = ScrollVisibility.Never;//垂直滚动条 this.treeList.OptionsView.FocusRectStyle = DrawFocusRectStyle.None;//焦点虚框 this.treeList.StateImageList = this.imageCollection;//图片集 this.treeList.OptionsView.ShowButtons = false;//显示展开按钮 this.treeList.TreeLevelWidth = 0;//凹槽宽 this.treeList.RowHeight = 30;//行高 this.ColumnPanelRowHeight = 30;// 表头高度 this.treeList.OptionsBehavior.AutoNodeHeight = false;// 自动行高 this.treeList.DataSource = new List<Dto>(); this.treeList.KeyFieldName = "Id"; this.treeList.ParentFieldName = "ParentId"; // 设置列是否可见 this.treeList.Columns["ImgName"].Visible = false; this.OptionsCustomization.AllowQuickHideColumns = false;// 是否可以拖动来隐藏列
展开当前节点
e.Node.Expanded = true;
CalcNodeHeight 单独设置子节点行高
private void TreeList_CalcNodeHeight(object sender, CalcNodeHeightEventArgs e) { if (!e.Node.Nodes.Any()) { e.NodeHeight = 30; } }
NodeCellStyle 重绘网格样式
e.Column.FieldName e.Appearance.ForeColor = ColorTranslator.FromHtml("#666666");
CustomColumnDisplayText 重绘网格显示的值
e.DisplayText = "自定义显示文本";
根据坐标 获取选中行
// 获取鼠标点击时选中的节点 或者直接根据X Y 坐标 new Point(x,y) TreeListNode currentNode = this.treeList.GetNodeAt(e.Point); // 设置焦点行 this.treeList.FocusedNode = currentNode; // 通过焦点行取数据 var focused = this.treeList.GetFocusedRow() as Dto;
GetStateImage 自带的行头绑定图片设置图片
private void TreeList_GetStateImage(object sender, GetStateImageEventArgs e) { // 获取所有图片对应的名称 var keys = this.imageCollection.Images.Keys; // 获取当前节点绑定的图片位置是第几个 e.NodeImageIndex = keys.IndexOf(e.Node.GetValue("ImgName").ToString()); }
TreeList 的封装
[ToolboxItem(true)] public class TreeListEx : TreeList { /// <summary> /// 右键菜单自定义事件 /// </summary> public event Action<object, EventArgs> CustomFunction; #region 列设置 /// <summary> /// 是否开启排序 /// </summary> public bool IsAllowSort { get; set; } = false; /// <summary> /// 是否允许窗体宽度足够自动列宽 /// </summary> public bool IsAutoWidthColumn { get; set; } = true; /// <summary> /// 排序列名集合 /// </summary> public List<string> SortColumnsList { get; set; } /// <summary> /// 只读列名集合 /// </summary> public List<string> ReadOnlyColumnsList { get; set; } /// <summary> /// 需要隐藏的列名List /// </summary> public List<string> InvisibleColumnsList { get; set; } /// <summary> ///需要显示的列名List /// </summary> public List<string> VisibleColumnsList { get; set; } /// <summary> ///需要添加汇总的列名List /// </summary> public List<string> SummaryColumnsList { get; set; } /// <summary> ///需要格式化的数值列名List /// </summary> public List<string> FormatColumnsList { get; set; } /// <summary> ///需要格式化的金额列名List /// </summary> public List<string> AmountFormatColumnsList { get; set; } /// <summary> /// 自定义宽度列名集合 /// </summary> public Dictionary<string, int> CustomWidthColumns { get; set; } /// <summary> /// 自定义列名显示文本 /// </summary> public Dictionary<string, string> CustomColumnsCaptionDic { get; set; } #endregion 列设置 public TreeListEx() { this.RowHeight = 30; this.ColumnPanelRowHeight = 30; this.OptionsBehavior.AutoNodeHeight = false; this.OptionsCustomization.AllowQuickHideColumns = false; this.NodeCellStyle += TreeListEx_NodeCellStyle; this.PopupMenuShowing += TreeListEx_PopupMenuShowing; this.KeyFieldName = nameof(TreeNodeBaseDto.Id); this.ParentFieldName = nameof(TreeNodeBaseDto.ParentId); } /// <summary> /// 设置只读列 字体、背景颜色 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TreeListEx_NodeCellStyle(object sender, GetCustomNodeCellStyleEventArgs e) { if (this.ReadOnlyColumnsList != null) { if (this.ReadOnlyColumnsList.Contains(e.Column.FieldName)) { e.Appearance.BackColor = ColorTranslator.FromHtml("#99f4f4f5"); } } } /// <summary> /// 右键菜单 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TreeListEx_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) { if (e.MenuType == TreeListMenuType.Node) { if (this.CustomFunction != null) { var dxm = new DXMenuItem(); dxm.Caption = Context.L("查看明细"); // 通过 Tag 传右键点击坐标出去 dxm.Tag = e.Point; dxm.Click += Dxm_OpenDetails_Click; e.Menu.Items.Add(dxm); } } } private void Dxm_OpenDetails_Click(object sender, EventArgs e) { this.CustomFunction?.Invoke(sender, e); } /// <summary> /// 设置数据源 /// </summary> /// <param name="dataSource"></param> public void SetDataSource(object dataSource) { // 主表设置数据源 this.DataSource = dataSource; this.InitColumnsStyle(); } /// <summary> /// 初始化列样式信息 /// </summary> private void InitColumnsStyle() { this.OptionsCustomization.AllowSort = IsAllowSort; //设置列只读 this.SetColumnsReadOnly(); //设置列汇总 this.AddColumnsSummary(); //设置列隐藏 if (this.InvisibleColumnsList != null) { this.SetColumnsInvisible(); } else if (this.VisibleColumnsList != null)//设置列显示 { this.SetColumnsVisible(); } //排序列 this.SortColumns(); //格式化列 this.FormatColumns(); //自定义宽度 this.SetColumnsWidth(); //设置列标题 this.SetColumnsCaption(); } /// <summary> /// 设置列头显示名称 /// </summary> private void SetColumnsCaption() { this.CustomColumnsCaptionDic?.ForEach(x => { var column = this.Columns.ColumnByFieldName(x.Key); if (column != null) { column.Caption = Context.L(x.Value); } }); } /// <summary> /// 设置列宽 /// </summary> private void SetColumnsWidth() { // 如果设置了自动列宽并没有填写自定义列宽 if (this.CustomWidthColumns == null) { this.OptionsView.AutoWidth = this.IsAutoWidthColumn; } // 根据显示列总宽度 判断是否自动列宽 为了根据屏幕大小 自动调整是否自动列宽 else if ((Context.MainForm.Width - 200) > this.CustomWidthColumns?.Sum(x => x.Value)) { this.OptionsView.AutoWidth = this.IsAutoWidthColumn; } else { this.OptionsView.AutoWidth = false; } // 如果不是自动列宽 且自定义列宽不为空 设置列宽 if (!this.OptionsView.AutoWidth) { // 如果自定义列为空 且不是自动列宽 if (this.CustomWidthColumns == null) { var columns = this.VisibleColumns.Select(x => x.FieldName).ToList(); columns.ForEach(c => { var column = this.Columns.ColumnByFieldName(c); if (column != null) { column.Width = 100; } }); } this.CustomWidthColumns?.ForEach(x => { var column = this.Columns.ColumnByFieldName(x.Key); if (column != null) { column.Width = x.Value; } }); } } /// <summary> /// 设置列数据格式化 /// </summary> private void FormatColumns() { this.FormatColumnsList?.ForEach(x => { var column = this.Columns.ColumnByFieldName(x); if (column != null) { column.Format.FormatType = DevExpress.Utils.FormatType.Numeric; column.Format.FormatString = "0.##"; var editor = new RepositoryItemTextEdit(); editor.EditFormat.FormatType = DevExpress.Utils.FormatType.Numeric; editor.EditFormat.FormatString = "0.##"; column.ColumnEdit = editor; } }); this.AmountFormatColumnsList?.ForEach(x => { var column = this.Columns.ColumnByFieldName(x); if (column != null) { column.Format.FormatType = DevExpress.Utils.FormatType.Numeric; column.Format.FormatString = "###,##0.00"; var editor = new RepositoryItemTextEdit(); editor.EditFormat.FormatType = DevExpress.Utils.FormatType.Numeric; editor.EditFormat.FormatString = "0.####"; column.ColumnEdit = editor; } }); } /// <summary> /// 设置列可见 /// </summary> private void SetColumnsVisible() { this.Columns?.ForEach(x => { x.Visible = this.VisibleColumnsList.Contains(x.FieldName); }); } /// <summary> /// 设置列隐藏 /// </summary> private void SetColumnsInvisible() { this.InvisibleColumnsList?.ForEach(x => { var column = this.Columns.ColumnByFieldName(x); if (column != null) { column.Visible = false; } }); } /// <summary> /// 设置列底部汇总 /// </summary> private void AddColumnsSummary() { if (this.SummaryColumnsList == null) { return; } this.OptionsView.ShowSummaryFooter = true; this.SummaryColumnsList.ForEach(x => { var column = this.Columns.ColumnByFieldName(x); if (column != null) { column.AllNodesSummary = true; column.SummaryFooter = SummaryItemType.Sum; column.SummaryFooterStrFormat = "{0:0.##}"; } }); } /// <summary> /// 设置列只读 /// </summary> private void SetColumnsReadOnly() { this.ReadOnlyColumnsList?.ForEach(x => { var column = this.Columns.ColumnByFieldName(x); if (column != null) { column.OptionsColumn.ReadOnly = true; } }); } /// <summary> /// 列排序 /// </summary> private void SortColumns() { if (this.SortColumnsList == null) { this.SortColumnsList = this.VisibleColumnsList; } int index = 0; if (this.OptionsSelection.MultiSelectMode == TreeListMultiSelectMode.RowSelect) { index++; } this.SortColumnsList?.ForEach(x => { var column = this.Columns.ColumnByFieldName(x); if (column != null && column.Visible) { column.VisibleIndex = index++; } }); } /// <summary> /// 双击表头 自动列宽 /// </summary> /// <param name="e"></param> protected override void OnMouseDown(MouseEventArgs e) { var treeHI = this.CalcHitInfo(e.Location); // 双击表头 自动列宽 if (!treeHI.InRowCell && e.Button == MouseButtons.Left && e.Clicks == 2) { this.BestFitColumns(); } if (e.Button == MouseButtons.Left) { // 左键点击一下 } if (e.Button == MouseButtons.Left && e.Clicks == 2) { // 左键点击两下 } if (e.Button == MouseButtons.Right) { // 右键点击一下 } base.OnMouseDown(e); } /// <summary> /// 递归获取传入id节点的所有子节点 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="id"></param> /// <param name="dataList"></param> /// <returns></returns> public List<T> GetChildernNode<T>(Guid id, List<T> dataList) where T : TreeNodeBaseDto { List<T> data = new List<T>(); var list = dataList.Where(x => x.ParentId == id).ToList(); if (list != null) { data.AddRange(list); list.ForEach(x => { data.AddRange(GetChildernNode(x.Id, dataList)); }); return data; } return new List<T>(); } }
TreeNodeBaseDto 基类
- 所有是 TreeList 的数据源的实体 建议继承于它
-
public class TreeNodeBaseDto { public Guid Id { get; set; } public Guid? ParentId { get; set; } }
封装的使用流程
1. 拖入封装好的控件 2. 设置列的一些需要格式化的信息 这些值都是和 Column 的 FieldName 相对应的 this.treeList.VisibleColumnsList = new List<string>{}; this.treeList.CustomWidthColumns = new Dictionary<string, int>(); this.treeList.FormatColumnsList = new List<string>(); 3. 绑定一些需要的事件 this.treeList.CustomColumnDisplayText += TreeList_CustomColumnDisplayText; this.treeList.NodeCellStyle += TreeList_NodeCellStyle; 4. 设置数据源 this.treeList.SetDataSource(new List<Dto>{});