DevExpress.XtraTreeList.TreeList 之 XtraTreeHelper

DevExpress.XtraTreeList.TreeList 完整辅助类之 XtraTreeHelper.cs
 
 
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using System.Windows.Forms;
using DevExpress.XtraTreeList.Columns;
using System.Collections;
using DevExpress.Utils;
using DevExpress.XtraTreeList.ViewInfo;

 

namespace Tools.WinForm.ClientUtility.Helpers
{

 

/// <summary>
/// XtraTree辅助类
/// </summary>
public class XtraTreeHelper
{

 

#region [BindXtraTree]

 

/// <summary>
/// 绑定树
/// </summary>
/// <typeparam name="T">数据列表</typeparam>
/// <param name="tree">树控件</param>
/// <param name="datas">数据</param>
/// <param name="selectData">选中数据</param>
/// <param name="expandLevel">展开层级,0折叠,1展开全部,2展开到第二级,3展开到第三级…</param>
public static void BindXtraTree<T>(TreeList tree, List<T> datas, T selectData, int expandLevel) where T : IXtraTreeData
{
if (tree != null && datas != null)
{
tree.BeginUpdate();
List<T> roots = GetRootDatas(datas);
AppendNodes(tree, roots, expandLevel, null);
SelectNode(tree, selectData);
tree.Tag = datas;
tree.EndUpdate();
}
}

 

/// <summary>
/// 添加节点
/// </summary>
static void AppendNodes<T>(TreeList tree, List<T> childs, int expandLevel, TreeListNode pNode) where T : IXtraTreeData
{
if (tree != null && childs != null && childs.Count > 0)
{
Cursor currentCursor = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
tree.BeginUnboundLoad();
TreeListNode node;
foreach (T child in childs)
{
List<object> colDatas = new List<object>();
foreach (TreeListColumn col in tree.Columns)
{
if (!string.IsNullOrEmpty(col.FieldName))
{
object fieldValue = child.GetPropertyValue(col.FieldName);
colDatas.Add(fieldValue);
}
}
node = tree.AppendNode(colDatas.ToArray(), pNode, child);
if (child.Children != null && child.Children.Count > 0)
{
AppendNodes(tree, child.Children, expandLevel, node);
}
node.Expanded = (expandLevel == 1) || (expandLevel > child.NodeLevel);

 

}
tree.EndUnboundLoad();
Cursor.Current = currentCursor;
}
}

 

static List<T> GetRootDatas<T>(List<T> dataList) where T : IXtraTreeData
{
if (dataList != null || dataList.Count > 1)
{
Dictionary<string, T> dataDic = new Dictionary<string, T>();
foreach (T data in dataList)
{
dataDic[data.NodeID] = data;
data.Children.Clear();
data.Parent = null;
}
foreach (T node in dataList)
{
if (dataDic.ContainsKey(node.NodeParentID))
{
dataDic[node.NodeParentID].Children.Add(node);
node.Parent = dataDic[node.NodeParentID];
}
}
List<T> roots = dataList.FindAll(data => { return data.Parent == null; });
foreach (T root in roots)
{
SetNodeLevel(root, 1);
}
return roots;
}
return dataList;
}

 

static void SetNodeLevel<T>(T data, int level) where T : IXtraTreeData
{
data.NodeLevel = level;
level++;
if (data.Children != null && data.Children.Count > 0)
{
foreach (T child in data.Children)
{
SetNodeLevel(child, level);
}
}
}

 

#endregion

 

#region [SelectNode]

 

/// <summary>
/// 选中节点数据
/// </summary>
/// <typeparam name="T">节点类型</typeparam>
/// <param name="tree">树控件</param>
/// <param name="data">选中数据</param>
public static void SelectNode<T>(TreeList tree, T data) where T : IXtraTreeData
{
if (data != null && !string.IsNullOrEmpty(data.NodeID))
{
SelectNode(tree, data.NodeID);
}
}

 

/// <summary>
/// 选中节点ID
/// </summary>
/// <param name="tree">树控件</param>
/// <param name="selectID">选中数据ID</param>
public static void SelectNode(TreeList tree, string selectID)
{
if (tree != null && tree.Nodes != null && tree.Nodes.Count > 0 && !string.IsNullOrEmpty(selectID))
{
tree.BeginUpdate();
SelectNodeNest(tree.Nodes, selectID, false);
tree.EndUpdate();
}
}

 

private static void SelectNodeNest(TreeListNodes nodes, string selectID, bool finded)
{
foreach (TreeListNode node in nodes)
{
IXtraTreeData data = node.Tag as IXtraTreeData;
if (data != null)
{
if (data.NodeID == selectID)
{
node.Selected = true;
finded = true;
}
}
else
{
throw new InvalidOperationException("只有通过XtraTreeHelper绑定的树才能调用此方法选中节点");
}
if (finded)
{
break;
}
else if (node.Nodes != null && node.Nodes.Count > 0)
{
SelectNodeNest(node.Nodes, selectID, finded);
}
}
}

 

#endregion

 

#region [FindNode]

 

/// <summary>
/// 选中节点数据
/// </summary>
/// <typeparam name="T">节点类型</typeparam>
/// <param name="tree">树控件</param>
/// <param name="data">选中数据</param>
public static TreeListNode FindNode<T>(TreeList tree, T data) where T : IXtraTreeData
{
return FindNode(tree, data.NodeID);
}

 

/// <summary>
/// 选中节点ID
/// </summary>
/// <param name="tree">树控件</param>
/// <param name="nodeID">选中数据ID</param>
public static TreeListNode FindNode(TreeList tree, string nodeID)
{
if (tree != null && tree.Nodes != null && tree.Nodes.Count > 0 && !string.IsNullOrEmpty(nodeID))
{
tree.BeginUpdate();
TreeListNode findNode = FindNodeNest(tree.Nodes, nodeID);
tree.EndUpdate();
return findNode;
}
return null;
}

 

private static TreeListNode FindNodeNest(TreeListNodes nodes, string nodeID)
{
foreach (TreeListNode node in nodes)
{
IXtraTreeData data = node.Tag as IXtraTreeData;
if (data != null)
{
if (data.NodeID.Equals(nodeID))
{
return node;
}
else if (node.Nodes != null && node.Nodes.Count > 0)
{
TreeListNode findNode = FindNodeNest(node.Nodes, nodeID);
if (findNode != null) { return findNode; }
}
}
else
{
throw new InvalidOperationException("只有通过XtraTreeHelper绑定的树才能调用此方法选中节点");
}
}
return null;
}

 

#endregion

 

#region [ExpandLevel]

 

/// <summary>
/// 展开树
/// </summary>
/// <typeparam name="T">数据列表</typeparam>
/// <param name="tree">树控件</param>
/// <param name="expandLevel">展开层级,0折叠,1展开全部,2展开到第二级,3展开到第三级…</param>
public static void ExpandTree<T>(TreeList tree, int expandLevel) where T : IXtraTreeData
{
if (tree == null) { throw new ArgumentException("TreeList为空"); }
tree.SuspendLayout();
tree.BeginUpdate();
if (expandLevel <= 0) { tree.CollapseAll(); }
else if (expandLevel == 1) { tree.ExpandAll(); }
else
{
ExpandTreeNest(tree.Nodes, expandLevel);
}
tree.EndUpdate();
tree.ResumeLayout(true);
}

 

/// <summary>
/// 遍历递归展开
/// </summary>
/// <param name="nodes">节点集合</param>
/// <param name="expandLevel">展开层级</param>
private static void ExpandTreeNest(TreeListNodes nodes, int expandLevel)
{
if (nodes != null && nodes.Count > 0)
{
foreach (TreeListNode node in nodes)
{
IXtraTreeData data = node.Tag as IXtraTreeData;
if (data != null && data.NodeLevel < expandLevel)
{
node.Expanded = true;
if (node.Nodes != null && node.Nodes.Count > 0 && data.NodeLevel < expandLevel - 1)
{
ExpandTreeNest(node.Nodes, expandLevel);
}
else if (node.Nodes != null && node.Nodes.Count > 0)
{
foreach (TreeListNode cNode in node.Nodes)
{
cNode.Expanded = false;
}
}
}
}
}
}

 

#endregion

 

#region [AppendNode]



/// <summary>
/// 添加节点数据
/// </summary>
/// <typeparam name="T">节点类型</typeparam>
/// <param name="tree">树控件</param>
/// <param name="data">数据</param>
/// <param name="afterWho">在哪个节点后</param>
/// <param name="select">是否选中添加数据</param>
public static TreeListNode AppendNode<T>(TreeList tree, T data, string afterWho, bool select) where T : IXtraTreeData
{
if (tree == null) { throw new ArgumentException("TreeList为空"); }
tree.BeginUpdate();
TreeListNode appendNode = null;
TreeListNode parentNode = null;
if (!string.IsNullOrEmpty(data.NodeParentID))
{
parentNode = FindNode(tree, data.NodeParentID);
}
List<object> colDatas = new List<object>();
foreach (TreeListColumn col in tree.Columns)
{
if (!string.IsNullOrEmpty(col.FieldName))
{
object fieldValue = data.GetPropertyValue(col.FieldName);
colDatas.Add(fieldValue);
}
}
if (parentNode != null)
{
T pData = (T)parentNode.Tag;
if (pData != null)
{
pData.Children.Add(data);
data.Parent = pData;
}
}
else
{
List<T> dataSource = tree.Tag as List<T>;
if (dataSource != null)
{
dataSource.Add(data);
}
}
appendNode = tree.AppendNode(colDatas.ToArray(), parentNode, data);
if (!string.IsNullOrEmpty(afterWho))
{
TreeListNode afterWhoNode = FindNode(tree, afterWho);
if (afterWhoNode != null && object.ReferenceEquals(afterWhoNode.ParentNode, parentNode))
{
// 增加顶级节点处理
TreeListNodes nodes = afterWhoNode.ParentNode == null ? tree.Nodes : parentNode.Nodes;
int afterWhoIndex = nodes.IndexOf(afterWhoNode);
tree.SetNodeIndex(appendNode, afterWhoIndex + 1);
}
}
if (select)
{
appendNode.Selected = true;
}
tree.Refresh();
tree.EndUpdate();
return appendNode;
}

 

#endregion

 

#region [InsertNode]



/// <summary>
/// 插入节点数据
/// </summary>
/// <typeparam name="T">节点类型</typeparam>
/// <param name="tree">树控件</param>
/// <param name="data">数据</param>
/// <param name="beforWho">在哪个节点前</param>
/// <param name="select">是否选中添加数据</param>
public static TreeListNode InsertNode<T>(TreeList tree, T data, string beforWho, bool select) where T : IXtraTreeData
{
if (tree == null) { throw new ArgumentException("TreeList为空"); }
tree.BeginUpdate();
TreeListNode appendNode = null;
TreeListNode parentNode = null;
if (!string.IsNullOrEmpty(data.NodeParentID))
{
parentNode = FindNode(tree, data.NodeParentID);
}
List<object> colDatas = new List<object>();
foreach (TreeListColumn col in tree.Columns)
{
if (!string.IsNullOrEmpty(col.FieldName))
{
object fieldValue = data.GetPropertyValue(col.FieldName);
colDatas.Add(fieldValue);
}
}
if (parentNode != null)
{
T pData = (T)parentNode.Tag;
if (pData != null)
{
pData.Children.Add(data);
data.Parent = pData;
}
}
else
{
List<T> dataSource = tree.Tag as List<T>;
if (dataSource != null)
{
dataSource.Add(data);
}
}
appendNode = tree.AppendNode(colDatas.ToArray(), parentNode, data);
if (!string.IsNullOrEmpty(beforWho))
{
TreeListNode afterWhoNode = FindNode(tree, beforWho);
if (afterWhoNode != null && object.ReferenceEquals(afterWhoNode.ParentNode, parentNode))
{
// 增加顶级节点处理
TreeListNodes nodes = afterWhoNode.ParentNode == null ? tree.Nodes : parentNode.Nodes;
int afterWhoIndex = nodes.IndexOf(afterWhoNode);
tree.SetNodeIndex(appendNode, afterWhoIndex);
}
}
if (select)
{
appendNode.Selected = true;
}
//tree.Refresh();
tree.EndUpdate();
return appendNode;
}

 

#endregion

 

#region [UpdateNode]

 

/// <summary>
/// 更新节点
/// </summary>
/// <typeparam name="T">节点类型</typeparam>
/// <param name="tree">树控件</param>
/// <param name="data">数据</param>
/// <param name="select">是否选中修改节点</param>
public static TreeListNode UpdateNode<T>(TreeList tree, T data, bool select) where T : IXtraTreeData
{
if (tree != null && data != null && !string.IsNullOrEmpty(data.NodeID))
{
tree.BeginUpdate();
TreeListNode findNode = InnerUpdateNode(tree, data);
//tree.Refresh();
tree.EndUpdate();
return findNode;
}
return null;
}

 

#endregion

 

#region UpdateNodeList

 

/// <summary>
/// 批量更新节点列表
/// </summary>
/// <typeparam name="T">节点类型</typeparam>
/// <param name="tree">树控件</param>
/// <param name="dataList">数据列表</param>
public static void UpdateNodeList<T>(TreeList tree, List<T> dataList) where T : IXtraTreeData
{
tree.BeginUpdate();
foreach (var data in dataList)
InnerUpdateNode(tree, data);
tree.Refresh();
tree.EndUpdate();
}

 

private static TreeListNode InnerUpdateNode<T>(TreeList tree, T data) where T : IXtraTreeData
{
if (tree != null && data != null && !string.IsNullOrEmpty(data.NodeID))
{
TreeListNode findNode = FindNode(tree, data.NodeID);
if (findNode != null)
{
T oldData = (T)findNode.Tag;
if (oldData != null)
{
data.NodeLevel = oldData.NodeLevel;
data.Parent = oldData.Parent;
// 保存原节点的子节点,避免在data == oldData的情况下被清空掉
List<IXtraTreeData> childrenNode = oldData.Children.ToList();
data.Children.Clear();
data.Children.AddRange(childrenNode);

 

ReplaceNodeData(findNode, data, oldData);
}
else
oldData = data;

 

foreach (TreeListColumn col in tree.Columns)
{
if (!string.IsNullOrEmpty(col.FieldName))
{
object fieldValue = data.GetPropertyValue(col.FieldName);
oldData.SetPropertyValue(col.FieldName, fieldValue);
findNode.SetValue(col.FieldName, fieldValue);
}
}
}

 

return findNode;
}
return null;
}

 

private static void ReplaceNodeData<T>(TreeListNode findNode, T data, T oldData) where T : IXtraTreeData
{
if (oldData.Parent != null)
{
var index = oldData.Parent.Children.IndexOf(oldData);
oldData.Parent.Children.RemoveAt(index);
oldData.Parent.Children.Insert(index, data);
}

 

foreach (var childNode in data.Children)
{
childNode.Parent = data;
}

 

findNode.Tag = data;
}

 

#endregion

 

#region [RemoveNode]

 

/// <summary>
/// 移除节点
/// </summary>
/// <param name="tree">树控件</param>
/// <param name="nodeID">节点ID</param>
public static void RemoveNode(TreeList tree, string nodeID)
{
if (tree != null && !string.IsNullOrEmpty(nodeID))
{
tree.BeginUpdate();
TreeListNode findNode;
if (tree != null && !string.IsNullOrEmpty(nodeID))
{
findNode = FindNode(tree, nodeID);
if (findNode != null)
{
if (findNode.ParentNode == null) // 顶级节点
{
var dataSource = tree.Tag as IList;
if (dataSource != null && findNode.Tag != null)
dataSource.Remove(findNode.Tag);
tree.Nodes.Remove(findNode);
}
else
{
var parent = findNode.ParentNode.Tag as IXtraTreeData;
if (parent != null && parent.Children != null && findNode.Tag is IXtraTreeData)
parent.Children.Remove(findNode.Tag as IXtraTreeData);
findNode.ParentNode.Nodes.Remove(findNode);
}
//tree.Refresh();
}
}
tree.EndUpdate();
}
}

 

#endregion

 

#region [MoveUpNode]

 

/// <summary>
/// 上移节点
/// </summary>
/// <param name="tree">Tree控件</param>
/// <param name="nodeID">上移节点ID</param>
public static void MoveUpNode(TreeList tree, string nodeID)
{
if (tree != null && !string.IsNullOrEmpty(nodeID))
{
tree.BeginUpdate();
TreeListNode findNode;
findNode = FindNode(tree, nodeID);
if (findNode != null)
{
var nodes = findNode.ParentNode == null ? tree.Nodes : findNode.ParentNode.Nodes;
TreeListNode prevNode = findNode.PrevNode;
if (prevNode != null && !ReferenceEquals(findNode, prevNode))
{
int index = nodes.IndexOf(findNode);
int prevIndex = nodes.IndexOf(prevNode);
tree.SetNodeIndex(findNode, prevIndex);
tree.SetNodeIndex(prevNode, index);
}
findNode.Selected = true;
MoveNodeDataUp(findNode);
tree.Refresh();
}
tree.EndUpdate();
}
}

 

/// <summary>
/// 向下移动指定节点的数据
/// </summary>
/// <param name="findNode"></param>
private static void MoveNodeDataUp(TreeListNode findNode)
{
if (findNode.ParentNode == null)
return;

 

var parentData = findNode.ParentNode.Tag as IXtraTreeData;
if (parentData == null)
return;

 

var children = parentData.Children;
var nodeData = findNode.Tag as IXtraTreeData;

 

if (children == null || nodeData == null)
return;

 

var index = children.IndexOf(nodeData);
if (index <= 0)
return;

 

children.Move(x => x == nodeData, index - 1);
}

 

#endregion

 

#region [MoveTop]

 

/// <summary>
/// 移动节点到顶部
/// </summary>
/// <param name="tree">Tree控件</param>
/// <param name="nodeID">上移节点ID</param>
public static void MoveTop(TreeList tree, string nodeID)
{
if (tree != null && !string.IsNullOrEmpty(nodeID))
{
tree.BeginUpdate();
TreeListNode findNode = FindNode(tree, nodeID);
if (findNode != null)
{
var brotherNodes = findNode.ParentNode == null ? tree.Nodes : findNode.ParentNode.Nodes;
var nodeIndex = brotherNodes.IndexOf(findNode);

 

tree.SetNodeIndex(findNode, 0);
foreach (TreeListNode treeListNode in brotherNodes.OfType<TreeListNode>().ToList())
{
var index = tree.GetNodeIndex(treeListNode);
if (index < nodeIndex && findNode != treeListNode)
tree.SetNodeIndex(treeListNode, index + 1);
}
MoveNodeDataTop(findNode);
findNode.Selected = true;
tree.Refresh();
}
tree.EndUpdate();
}
}

 

/// <summary>
/// 向下移动指定节点的数据
/// </summary>
/// <param name="findNode"></param>
private static void MoveNodeDataTop(TreeListNode findNode)
{
if (findNode.ParentNode == null)
return;

 

var parentData = findNode.ParentNode.Tag as IXtraTreeData;
if (parentData == null)
return;

 

var children = parentData.Children;
var nodeData = findNode.Tag as IXtraTreeData;

 

if (children == null || nodeData == null)
return;

 

children.MoveToBeginning(x => x == nodeData);
}

 

#endregion

 

#region [MoveBottom]

 

/// <summary>
/// 移动节点到底部
/// </summary>
/// <param name="tree">Tree控件</param>
/// <param name="nodeID">上移节点ID</param>
public static void MoveBottom(TreeList tree, string nodeID)
{
if (tree != null && !string.IsNullOrEmpty(nodeID))
{
tree.BeginUpdate();
TreeListNode findNode = FindNode(tree, nodeID);
if (findNode != null)
{
var brotherNodes = findNode.ParentNode == null ? tree.Nodes : findNode.ParentNode.Nodes;
var nodeIndex = brotherNodes.IndexOf(findNode);

 

tree.SetNodeIndex(findNode, brotherNodes.Count - 1);
foreach (TreeListNode treeListNode in brotherNodes.OfType<TreeListNode>().ToList())
{
var index = tree.GetNodeIndex(treeListNode);
if (index > nodeIndex && findNode != treeListNode)
tree.SetNodeIndex(treeListNode, index - 1);
}

 

MoveNodeDataBottom(findNode);
findNode.Selected = true;
tree.Refresh();
}
tree.EndUpdate();
}
}

 

/// <summary>
/// 移动指定节点的数据到底部
/// </summary>
/// <param name="findNode"></param>
private static void MoveNodeDataBottom(TreeListNode findNode)
{
if (findNode.ParentNode == null)
return;

 

var parentData = findNode.ParentNode.Tag as IXtraTreeData;
if (parentData == null)
return;

 

var children = parentData.Children;
var nodeData = findNode.Tag as IXtraTreeData;
if (children == null || nodeData == null)
return;

 

children.MoveToEnd(x => x == nodeData);
}

 

#endregion

 

#region [MoveDownNode]

 

/// <summary>
/// 下移节点
/// </summary>
/// <param name="tree"></param>
/// <param name="nodeID"></param>
public static void MoveDownNode(TreeList tree, string nodeID)
{
if (tree != null && !string.IsNullOrEmpty(nodeID))
{
tree.BeginUpdate();
TreeListNode findNode = FindNode(tree, nodeID);
if (findNode != null)
{
var nodes = findNode.ParentNode == null ? tree.Nodes : findNode.ParentNode.Nodes;
TreeListNode nextNode = findNode.NextNode;
if (nextNode != null && !ReferenceEquals(findNode, nextNode))
{
int index = nodes.IndexOf(findNode);
int nextIndex = nodes.IndexOf(nextNode);
tree.SetNodeIndex(findNode, nextIndex);
tree.SetNodeIndex(nextNode, index);
}

 

MoveNodeDataDown(findNode);
findNode.Selected = true;
tree.Refresh();
}
tree.EndUpdate();
}
}

 

/// <summary>
/// 向下移动指定节点的数据
/// </summary>
/// <param name="findNode"></param>
private static void MoveNodeDataDown(TreeListNode findNode)
{
if (findNode.ParentNode == null)
return;

 

var parentData = findNode.ParentNode.Tag as IXtraTreeData;
if (parentData == null)
return;

 

var children = parentData.Children;
var nodeData = findNode.Tag as IXtraTreeData;

 

if (children == null || nodeData == null)
return;

 

var index = children.IndexOf(nodeData);
if (index < 0 || index == children.Count - 1)
return;

 

children.Move(x => x == nodeData, index + 1);
}

 

#endregion

 

/// <summary>
/// 固定列宽
/// </summary>
/// <param name="column">列</param>
/// <param name="width">宽度</param>
public static void FixTreeListColumnWidth(TreeListColumn column, int width)
{
column.MinWidth = column.Width = width;
column.OptionsColumn.FixedWidth = true;
}

 

/// <summary>
/// 向TreeList注册单元格中的提示信息
/// 用户可根据节点,列来指定提示内容
/// </summary>
/// <typeparam name="TEntity">treeList上绑定的实体的类型</typeparam>
/// <param name="treeList">要注册的TreeList</param>
/// <param name="showTipsCondition">显示提示信息的条件</param>
/// <param name="getCellHintText">获取显示文本的委托</param>
/// <param name="getCellHintTitle">获取显示标题的委托,如果为空则显示列标题</param>
public static void RegisteTreeCellHint<TEntity>(TreeList treeList, Func<TreeListHitInfo, bool> showTipsCondition, Func<TEntity, string> getCellHintText, Func<TreeListNode, TreeListColumn, string> getCellHintTitle = null)
where TEntity : class
{
RegisteTreeCellHint(treeList, showTipsCondition,
(node, column) => getCellHintText(treeList.GetDataRecordByNode(node) as TEntity),
getCellHintTitle
);
}

 

/// <summary>
/// 向TreeList注册单元格中的提示信息
/// 用户可根据节点,列来指定提示内容
/// </summary>
/// <param name="treeList">要注册的TreeList</param>
/// <param name="showTipsCondition">显示提示信息的条件</param>
/// <param name="getCellHintText">获取显示文本的委托</param>
/// <param name="getCellHintTitle">获取显示标题的委托,如果为空则显示列标题</param>
public static void RegisteTreeCellHint(TreeList treeList, Func<TreeListHitInfo, bool> showTipsCondition, Func<TreeListNode, TreeListColumn, string> getCellHintText, Func<TreeListNode, TreeListColumn, string> getCellHintTitle = null)
{
if (treeList == null)
throw new ArgumentNullException("treeList");
if (showTipsCondition == null)
throw new ArgumentNullException("showTipsCondition");
if (getCellHintText == null)
throw new ArgumentNullException("getCellHintText");

 

ToolTipController toolTipController = treeList.ToolTipController;
if (toolTipController == null)
{
toolTipController = new ToolTipController { ToolTipType = ToolTipType.SuperTip };
treeList.ToolTipController = toolTipController;
}

 

toolTipController.GetActiveObjectInfo += (sender, e) =>
{
if (e.SelectedControl != treeList) return;
ToolTipControlInfo info = e.Info;
try
{
var hi = treeList.CalcHitInfo(e.ControlMousePosition);
if (hi.HitInfoType == HitInfoType.Cell && showTipsCondition(hi))
{
var hintText = getCellHintText(hi.Node, hi.Column);
var hintTitle = getCellHintTitle == null ? hi.Column.Caption : getCellHintTitle(hi.Node, hi.Column);
info = new ToolTipControlInfo(new TreeListCellToolTipInfo(hi.Node, hi.Column, null), hintText, hintTitle);
}
}
finally
{
e.Info = info;
}
};
}

 

}
/// <summary>
/// Object扩展方法
/// </summary>
public static class ObjectExtends
{
#region [SetPropertyValue]

 

/// <summary>
/// 动态设置属性值
/// </summary>
/// <param name = "obj">要设置属性的对象</param>
/// <param name = "propertyName">属性名</param>
/// <param name = "value">值</param>
public static void SetPropertyValue(this object obj, string propertyName, object value)
{
var type = obj.GetType();
var property = type.GetProperty(propertyName);

 

if (property == null)
throw new ArgumentException(string.Format("属性'{0}'不存在.", propertyName), propertyName);
if (!property.CanWrite)
throw new ArgumentException(string.Format("属性'{0}'不可写.", propertyName), propertyName);
property.SetValue(obj, value, null);
}

 

#endregion

 

#region [GetPropertyValue]

 

/// <summary>
/// 通过反射动态获取属性值
/// </summary>
/// <param name = "obj">持有属性的对象</param>
/// <param name = "propertyName">属性名</param>
/// <returns>属性值</returns>
public static object GetPropertyValue(this object obj, string propertyName)
{
return GetPropertyValue<object>(obj, propertyName, null);
}



/// <summary>
/// 取得强类型属性值
/// </summary>
/// <typeparam name = "T">返回值类型</typeparam>
/// <param name = "obj">持有属性的对象</param>
/// <param name="defaultValue">默认值</param>
/// <param name = "propertyName">属性名</param>
/// <returns>属性值</returns>
public static T GetPropertyValue<T>(this object obj, string propertyName, T defaultValue)
{
var type = obj.GetType();
var property = type.GetProperty(propertyName);

 

if (property == null)
throw new ArgumentException(string.Format("属性'{0}'不存在.", propertyName), propertyName);

 

var value = property.GetValue(obj, null);
return (value is T ? (T)value : defaultValue);
}

 

#endregion

 

#region [IsType]

 

/// <summary>
/// 对象是否为T类型
/// </summary>
public static bool IsType<T>(this object obj)
{
Type t = typeof(T);
return (obj.GetType().Equals(t));
}

 

/// <summary>
/// 取得对应类型默认值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static T GetTypeDefaultValue<T>(this T value)
{
return default(T);
}

 

#endregion

 

}

 

/// <summary>
/// <see cref="System.Collections.Generic.List{T}"/> 扩展方法
/// </summary>
public static class ListExtensions
{
/// <summary>
/// Moves the item matching the <paramref name="itemSelector"/> to the end of the <paramref name="list"/>.
/// </summary>
public static void MoveToEnd<T>(this List<T> list, Predicate<T> itemSelector)
{
if (list == null)
throw new ArgumentException(string.Format("参数'{0}'等于null。", list));
if (list.Count > 1)
list.Move(itemSelector, list.Count - 1);
}

 

/// <summary>
/// Moves the item matching the <paramref name="itemSelector"/> to the beginning of the <paramref name="list"/>.
/// </summary>
public static void MoveToBeginning<T>(this List<T> list, Predicate<T> itemSelector)
{
if (list == null)
throw new ArgumentException(string.Format("参数'{0}'等于null。", list));
list.Move(itemSelector, 0);
}

 

/// <summary>
/// 上移
/// </summary>
public static void MoveUp<T>(this List<T> list, T item)
{
if (list==null || list.Count==0 || item == null)
return;

 

var currIndex = list.IndexOf(item);

 

if (currIndex <= 0)
return;

 

// 交换当前元素与上一元素
list[currIndex] = list[currIndex - 1];
list[currIndex - 1] = item;
}

 

/// <summary>
/// 下移
/// </summary>
public static void MoveDown<T>(this List<T> list, T item)
{
if (list == null || list.Count == 0 || item == null)
return;

 

var currIndex = list.IndexOf(item);

 

if (currIndex < 0 || currIndex == list.Count - 1)
return;

 

// 交换当前元素与下一元素
list[currIndex] = list[currIndex + 1];
list[currIndex + 1] = item;
}

 

/// <summary>
/// Moves the item matching the <paramref name="itemSelector"/> to the <paramref name="newIndex"/> in the <paramref name="list"/>.
/// </summary>
public static void Move<T>(this List<T> list, Predicate<T> itemSelector, int newIndex)
{
if (list == null)
throw new ArgumentException(string.Format("参数'{0}'等于null。", list));
if (itemSelector == null)
throw new ArgumentException(string.Format("参数'{0}'等于null。", itemSelector));
if (newIndex < 0)
return;

 

var currentIndex = list.FindIndex(itemSelector);
if (currentIndex < 0)
return;

 

if (currentIndex == newIndex)
return;

 

// Copy the item
var item = list[currentIndex];

 

// Remove the item from the list
list.RemoveAt(currentIndex);

 

// Finally insert the item at the new index
list.Insert(newIndex, item);
}
}
}
posted @ 2019-01-16 15:02  学习者编程  阅读(566)  评论(0编辑  收藏  举报