using DevExpress.XtraTreeList;
|
using DevExpress.XtraTreeList.Columns;
|
using DevExpress.XtraTreeList.Nodes;
|
using System.Collections;
|
|
namespace Verify
|
{
|
public static class TreeListExtend
|
{
|
/// <summary>
|
/// 根据clinet point 获取节点
|
/// </summary>
|
/// <param name="rhs"></param>
|
/// <param name="cp"></param>
|
/// <returns></returns>
|
public static TreeListNode GetNodeByCP(this TreeList rhs, Point cp)
|
{
|
Point pt = rhs.PointToClient(cp);
|
TreeListHitInfo ht = rhs.CalcHitInfo(pt);
|
TreeListNode node = ht.Node;
|
if (node is TreeListAutoFilterNode)
|
return null;
|
return node;
|
}
|
|
/// <summary>
|
/// 初始化默认设置
|
/// </summary>
|
/// <param name="rhs"></param>
|
/// <param name="height"></param>
|
public static void InitialDefaultSettings(this TreeList rhs, int height = 30)
|
{
|
rhs.OptionsBehavior.Editable = false;
|
rhs.OptionsBehavior.ReadOnly = true;
|
rhs.OptionsSelection.EnableAppearanceFocusedCell = false;
|
rhs.OptionsMenu.EnableNodeMenu = false;
|
rhs.OptionsView.FocusRectStyle = DrawFocusRectStyle.None;
|
rhs.OptionsSelection.SelectNodesOnRightClick = true;
|
rhs.ViewStyle = TreeListViewStyle.TreeView;
|
rhs.RowHeight = height;
|
|
rhs.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
|
rhs.OptionsFind.ShowCloseButton = false;
|
}
|
|
/// <summary>
|
/// 初始化多选框设置
|
/// </summary>
|
/// <param name="rhs"></param>
|
/// <param name="checkBoxFieldName"></param>
|
/// <param name="height"></param>
|
public static void InitialDefaultMultiSelectSettings(this TreeList rhs, string checkBoxFieldName = "Checked", int height = 30)
|
{
|
rhs.InitialDefaultSettings(height);
|
rhs.CheckBoxFieldName = checkBoxFieldName;//多选框绑定列名
|
rhs.OptionsBehavior.AllowRecursiveNodeChecking = true;//获取或设置父节点时是否自动检查/取消检查子节点
|
rhs.OptionsView.CheckBoxStyle = DevExpress.XtraTreeList.DefaultNodeCheckBoxStyle.Check;//设置勾选框样式
|
}
|
|
/// <summary>
|
/// 初始化可编辑视图
|
/// </summary>
|
public static void InitialBindingNormalEditView(this TreeList rhs, int height = 30)
|
{
|
rhs.OptionsSelection.EnableAppearanceFocusedCell = false;
|
rhs.OptionsMenu.EnableNodeMenu = false;
|
rhs.OptionsView.FocusRectStyle = DrawFocusRectStyle.None;
|
rhs.OptionsSelection.SelectNodesOnRightClick = true;
|
rhs.OptionsMenu.EnableColumnMenu = false;
|
rhs.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
|
rhs.Appearance.Row.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Near;
|
rhs.RowHeight = height;
|
}
|
|
/// <summary>
|
/// 多列显示设置
|
/// </summary>
|
/// <param name="rhs"></param>
|
/// <param name="height"></param>
|
public static void InitialMultiColSettings(this TreeList rhs, int height = 30)
|
{
|
rhs.OptionsBehavior.Editable = false;
|
rhs.OptionsBehavior.ReadOnly = true;
|
rhs.OptionsSelection.EnableAppearanceFocusedCell = false;
|
rhs.OptionsMenu.EnableNodeMenu = false;
|
rhs.OptionsView.FocusRectStyle = DrawFocusRectStyle.None;
|
rhs.OptionsSelection.SelectNodesOnRightClick = true;
|
rhs.OptionsMenu.EnableColumnMenu = false;
|
rhs.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
|
rhs.Appearance.Row.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
|
rhs.RowHeight = height;
|
|
rhs.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
|
rhs.OptionsFind.ShowCloseButton = false;
|
}
|
|
/// <summary>
|
/// 初始化多选框设置
|
/// </summary>
|
/// <param name="rhs"></param>
|
/// <param name="checkBoxFieldName"></param>
|
/// <param name="height"></param>
|
public static void InitialMultiColMultiSelectSettings(this TreeList rhs, string checkBoxFieldName = "Checked", int height = 30)
|
{
|
rhs.InitialMultiColSettings(height);
|
rhs.CheckBoxFieldName = checkBoxFieldName;//多选框绑定列名
|
rhs.OptionsBehavior.AllowRecursiveNodeChecking = true;//获取或设置父节点时是否自动检查/取消检查子节点
|
rhs.OptionsView.CheckBoxStyle = DefaultNodeCheckBoxStyle.Check;//设置勾选框样式
|
}
|
|
|
//渐变色
|
public static void RegistCustomDrawNodeCell(this TreeList rhs, Color? bgColor = null, Color? foreColor = null)
|
{
|
if (bgColor == null)
|
bgColor = Color.FromArgb(0, 122, 204);
|
if (foreColor == null)
|
foreColor = Color.White;
|
rhs.CustomDrawNodeCell += (sender, e) =>
|
{
|
if (e.Node == (sender as TreeList).FocusedNode)
|
{
|
e.Appearance.BackColor = bgColor.Value;
|
e.Appearance.ForeColor = foreColor.Value;
|
}
|
};
|
}
|
|
/// <summary>
|
/// 获取当前数据行
|
/// </summary>
|
public static T GetCurrentViewModel<T>(this TreeList tree, IEnumerable<T> source) where T : class
|
{
|
if (source == null)
|
return default;
|
if (source.Count() < 1)
|
{
|
return default;
|
}
|
var row = tree.GetFocusedRow() as T;
|
if (row == null)
|
{
|
return null;
|
}
|
return row;
|
}
|
|
|
/*/// <summary>
|
/// 导出Excel
|
/// </summary>
|
public static void ExportExcel(this TreeList tree, DocumentPage page, string pageOperateInfo)
|
{
|
page.ShowCmdOperateInfo("导出列表信息到Excel");
|
var path = ExcelSaveFilePathHelper.SaveFilePathName();
|
page.ShowCmdOperateInfo(pageOperateInfo);
|
if (string.IsNullOrEmpty(path)) return;
|
tree.OptionsPrint.PrintAllNodes = true;//导出明细
|
tree.OptionsPrint.PrintTree = true;//导出所有明细,false的话,只会导出展开的明细
|
tree.ExportToXlsx(path, new DevExpress.XtraPrinting.XlsxExportOptionsEx() { ExportType = DevExpress.Export.ExportType.WYSIWYG, TextExportMode = DevExpress.XtraPrinting.TextExportMode.Text });
|
DialogResult dr = XtraMessageBox.Show("导出成功!是否打开刚刚保存的Excel文件?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
|
if (dr != DialogResult.OK)
|
return;
|
System.Diagnostics.Process.Start(path);
|
}*/
|
|
/// <summary>
|
/// 设置拖拽状态下的普通视图
|
/// </summary>
|
public static void SetDragViewModels<t>(this TreeList rhs) where t : class
|
{
|
rhs.DragDrop += (sender, e) =>
|
{
|
TreeListNode node = (TreeListNode)e.Data.GetData(typeof(TreeListNode));
|
if (node == null) return;
|
TreeList list = (TreeList)sender;
|
if (list == node.TreeList) return;
|
TreeListHitInfo info = list.CalcHitInfo(list.PointToClient(new Point(e.X, e.Y)));
|
InsertBrush(list, node, info.Node == null ? -1 : info.Node.Id);
|
};
|
}
|
|
private static void InsertBrush(TreeList list, TreeListNode node, int parent)
|
{
|
ArrayList data = new ArrayList();
|
foreach (TreeListColumn column in node.TreeList.Columns)
|
{
|
data.Add(node[column]);
|
}
|
parent = list.AppendNode(data.ToArray(), parent).Id;
|
|
if (node.HasChildren)
|
foreach (TreeListNode n in node.Nodes)
|
InsertBrush(list, n, parent);
|
}
|
|
|
|
#region 刷新选中节点
|
private static List<TreeListNode> _haveChildNodes = null;
|
public static void RefreshCheckeState(this TreeList rhs)
|
{
|
if (rhs.OptionsView.CheckBoxStyle != DefaultNodeCheckBoxStyle.Check)
|
return;
|
_haveChildNodes = new List<TreeListNode>();
|
foreach (TreeListNode node in rhs.Nodes)
|
{
|
GetHaveChildNode(node);
|
}
|
if (_haveChildNodes != null && _haveChildNodes.Count > 0)
|
{
|
_haveChildNodes.Reverse();
|
foreach (var node in _haveChildNodes)
|
{
|
if (node.Nodes != null && node.Nodes.Any())
|
{
|
foreach (var childNode in node.Nodes)
|
{
|
|
}
|
var checkCount = node.Nodes.Where(x => x.Checked).Count();
|
if (checkCount == 0)
|
{
|
node.CheckState = CheckState.Unchecked;
|
}
|
else if (checkCount == node.Nodes.Count)
|
{
|
node.CheckState = CheckState.Checked;
|
}
|
else
|
{
|
node.CheckState = CheckState.Indeterminate;
|
}
|
}
|
}
|
}
|
_haveChildNodes = null;
|
}
|
|
private static void GetHaveChildNode(TreeListNode node)
|
{
|
if (node.Nodes.Count > 0)
|
{
|
_haveChildNodes.Add(node);
|
|
foreach (TreeListNode childNode in node.Nodes)
|
{
|
GetHaveChildNode(childNode);
|
}
|
}
|
}
|
#endregion
|
|
}
|
}
|
|
|
|
/*/// <summary>
|
/// 设置拖拽状态下的普通视图
|
/// </summary>
|
public static void SetDragViewModels<t>(this TreeList rhs) where t : class
|
{
|
rhs.DragDrop += (sender, e) =>
|
{
|
TreeListNode node = (TreeListNode)e.Data.GetData(typeof(TreeListNode));
|
if (node == null) return;
|
TreeList list = (TreeList)sender;
|
if (list == node.TreeList) return;
|
TreeListHitInfo info = list.CalcHitInfo(list.PointToClient(new Point(e.X, e.Y)));
|
InsertBrush(list, node, info.Node == null ? -1 : info.Node.Id);
|
};
|
}
|
|
private static void InsertBrush(TreeList list, TreeListNode node, int parent)
|
{
|
ArrayList data = new ArrayList();
|
foreach (TreeListColumn column in node.TreeList.Columns)
|
{
|
data.Add(node[column]);
|
}
|
parent = list.AppendNode(data.ToArray(), parent).Id;
|
|
if (node.HasChildren)
|
foreach (TreeListNode n in node.Nodes)
|
InsertBrush(list, n, parent);
|
}*/
|