namespace Yw.WinFrmUI
|
{
|
/// <summary>
|
///
|
/// </summary>
|
public static class TreeListExtensions
|
{
|
/// <summary>
|
/// 根据 client point 获取节点
|
/// </summary>
|
public static TreeListNode GetNodeByCP(this TreeList rhs, Point cp)
|
{
|
Point pt = rhs.PointToClient(cp);
|
DevExpress.XtraTreeList.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 = DevExpress.XtraTreeList.DrawFocusRectStyle.None;
|
rhs.OptionsSelection.SelectNodesOnRightClick = true;
|
rhs.ViewStyle = TreeListViewStyle.TreeView;
|
|
rhs.RowHeight = height;
|
}
|
|
/// <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)
|
{
|
InitialDefaultSettings(rhs, 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 = DevExpress.XtraTreeList.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 = DevExpress.XtraTreeList.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;
|
}
|
|
/// <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)
|
{
|
InitialDefaultSettings(rhs, height);
|
rhs.CheckBoxFieldName = checkBoxFieldName;//多选框绑定列名
|
rhs.OptionsBehavior.AllowRecursiveNodeChecking = true;//获取或设置父节点时是否自动检查/取消检查子节点
|
rhs.OptionsView.CheckBoxStyle = DevExpress.XtraTreeList.DefaultNodeCheckBoxStyle.Check;//设置勾选框样式
|
}
|
|
/// <summary>
|
/// 渐变色
|
/// </summary>
|
/// <param name="rhs"></param>
|
/// <param name="bgColor"></param>
|
/// <param name="foreColor"></param>
|
public static void RegistCustomDrawNodeCell(this TreeList rhs, System.Drawing.Color? bgColor = null, System.Drawing.Color? foreColor = null)
|
{
|
if (bgColor == null)
|
bgColor = System.Drawing.Color.FromArgb(0, 122, 204);
|
if (foreColor == null)
|
foreColor = System.Drawing.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>
|
/// 设置拖拽状态下的普通视图
|
/// </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);
|
}
|
}
|
}
|