本帖最后由 贾林朋 于 2014-9-21 00:21 编辑
treelist 的扩展方法:,
禁用CheckBox
[C#] 纯文本查看 复制代码 /// <summary>
/// 禁用CheckBox
/// 说明
/// 在CustomDrawNodeCheckBox事件中使用
/// Predicate<T>委托在.NET类类库中经常出现,此委托的定义如下:
/// public delegatebool Predicate<T>(T obj);
/// 从其定义可以看到,此委托引用一个返回bool 值的方法,在实际开发中,通常使用Predicate<T>委托变量引用一个“判断条件函数”,
/// 在判断条件函数内部书写代码表明函数参数所引用的对象应满足的条件,条件满足时,函数返回true.
/// </summary>
/// <param name="tree">TreeList</param>
/// <param name="conditionHanlder">委托</param>
/// <param name="e">CustomDrawNodeCheckBoxEventArgs</param>
public static void DisabledCheckBox(this TreeListNode tree, Predicate<TreeListNode> conditionHanlder, CustomDrawNodeCheckBoxEventArgs e)
{
if (conditionHanlder(e.Node))
{
e.ObjectArgs.State = DevExpress.Utils.Drawing.ObjectState.Disabled;
}
}
2禁止操作节点CheckBox
[C#] 纯文本查看 复制代码 /// <summary>
/// 禁止操作节点CheckBox
/// 说明
/// 在BeforeCheckNode事件中使用
/// </summary>
/// <param name="tree">TreeListNode</param>
/// <param name="conditionHanlder">委托</param>
/// <param name="e">CheckNodeEventArgs</param>
public static void DisabledSetCheckBox(this TreeListNode tree, Predicate<TreeListNode> conditionHanlder, CheckNodeEventArgs e)
{
if (conditionHanlder(e.Node))
{
e.CanCheck = false;
}
}
3 添加提示
[C#] 纯文本查看 复制代码 /// <summary>
///
/// 为节点提供Tooltip
/// 说明:
/// 1.设置tree.ToolTipController属性
/// 2.ToolTipController的GetActiveObjectInfo事件中使用
/// 3.举例
/// tlLHData.CustomNodeTooltip(e, node =>
///{
/// string _cabId = node.GetKeyID();
/// CCabInfo _cabinfo = LHDBHelper.GetCabInfo(_cabId);
/// if (_cabinfo != null)
/// {
/// return string.Format("核对时间:{0}\r\n在线情况:{1}\r\n最后一次活跃时间:{2}\r\n",
/// _cabinfo.ChkDataTime,
/// _cabinfo.CtuOnlineStatus == 1 ? "在线" : "未上线",
/// _cabinfo.LastAliveTime);
/// }
/// return string.Empty;
///});
/// </summary>
/// <param name="tree">TreeList</param>
/// <param name="e">ToolTipControllerGetActiveObjectInfoEventArgs</param>
/// <param name="builderNodeTooltipHandler">委托</param>
public static void CustomNodeTooltip(this TreeList tree, ToolTipControllerGetActiveObjectInfoEventArgs e, Func<TreeListNode, string> builderNodeTooltipHandler)
{
if (e.SelectedControl is DevExpress.XtraTreeList.TreeList)
{
TreeList _tree = (TreeList)e.SelectedControl;
TreeListHitInfo _hit = _tree.CalcHitInfo(e.ControlMousePosition);
if (_hit.HitInfoType == HitInfoType.Cell)
{
TreeListViewInfo _viewInfo = _tree.ViewInfo;
RowInfo _rowInfo = _viewInfo.GetRowInfoByPoint(e.ControlMousePosition);
CellInfo _cellInfo = _rowInfo.Cells[_hit.Column.VisibleIndex] as CellInfo;
EditHitInfo _editHitInfo = _cellInfo.EditorViewInfo.CalcHitInfo(e.ControlMousePosition);
if (_editHitInfo.HitTest == EditHitTest.MaskBox)
{
string _toolTip = builderNodeTooltipHandler(_hit.Node);
if (!string.IsNullOrEmpty(_toolTip))
e.Info = new ToolTipControlInfo(_cellInfo, _toolTip);
}
}
}
}
4 设置图片节点的背景色
[C#] 纯文本查看 复制代码 /// <summary>
/// 设置图片节点的背景色
/// 说明:在CustomDrawNodeImages事件中使用
/// </summary>
/// <param name="tree">TreeList</param>
/// <param name="e">CustomDrawNodeImagesEventArgs</param>
/// <param name="builderBackColorHandler">委托</param>
public static void CustomImageNodeBackColor(this TreeList tree, CustomDrawNodeImagesEventArgs e, Func<TreeListNode, Color> builderBackColorHandler)
{
TreeListNode _node = e.Node;
Color _backColor = builderBackColorHandler(_node);
e.Graphics.FillRectangle(new SolidBrush(_backColor), e.Bounds);
}
5向上递归,获取符合条件的父节点
/// <summary>
/// 向上递归,获取符合条件的父节点
/// 调用方法:TreeListNode _condionParent = _node.GetParentNode(n => n.GetNodeType() == NodeType.Cab);//获取类型为CAB类型的父节点
/// </summary>
/// <param name="node">需要向上递归的节点</param>
/// <param name="conditionHanlder">判断条件【委托】</param>
/// <returns>符合条件的节点【TreeListNode】</returns>
public static TreeListNode GetParentNode(this TreeListNode node, Predicate<TreeListNode> conditionHanlder)
{
TreeListNode _parentNode = node.ParentNode;//获取上一级父节点
TreeListNode _conditonNode = null;
if (_parentNode != null)
{
if (conditionHanlder(_parentNode))//判断上一级父节点是否符合要求
{
_conditonNode = _parentNode;
}
if (_conditonNode == null)//若没有找到符合要求的节点,递归继续
_conditonNode = GetParentNode(_parentNode, conditionHanlder);
}
return _conditonNode;
}
以上采用的是扩展方法 实现treelist control 内的功能,如对于扩展方法不太了解可以参考c# 扩展方法奇思妙用
|