下面的示例代码展示了如何创建一个引入了 Height 属性的自定义节点类。 此属性允许为个别节点设置高度。
下面的插图展示了运行结果。
C# | 复制代码 |
---|---|
using DevExpress.XtraTreeList; using DevExpress.XtraTreeList.Nodes; private void InitializeComponent() { // ... this.treeList1 = new MyTreeList(); // ... } private void Form1_Load(object sender, System.EventArgs e) { // ... ((MyTreeListNode)treeList1.Nodes[0]).Height = 25; // ... } public class MyTreeList : TreeList { protected override TreeListNode CreateNode(int nodeID, TreeListNodes owner) { return new MyTreeListNode(nodeID, owner); } protected override void InternalNodeChanged(TreeListNode node, NodeChangeTypeEnum changeType) { if(changeType == NodeChangeTypeEnum.User1) LayoutChanged(); base.InternalNodeChanged(node, changeType); } protected override void RaiseCalcNodeHeight(TreeListNode node, ref int nodeHeight) { MyTreeListNode myNode = node as MyTreeListNode; if(myNode != null) nodeHeight = myNode.Height; else base.RaiseCalcNodeHeight(node, ref nodeHeight); } public virtual int DefaultNodesHeight { get { return 18; } } } public class MyTreeListNode : TreeListNode { const int minHeight = 5; int height; public MyTreeListNode(int id, TreeListNodes owner) : base(id, owner) { this.height = (owner.TreeList as MyTreeList).DefaultNodesHeight; } public int Height { get { return height; } set { if(Height == value || value < minHeight) return; height = value; Changed(NodeChangeTypeEnum.User1); } } } |
Visual Basic | 复制代码 |
---|---|
Imports DevExpress.XtraTreeList Imports DevExpress.XtraTreeList.Nodes <System.Diagnostics.DebuggerStepThrough()>Private Sub InitializeComponent() Me.TreeList1 = New MyTreeList() ' ... End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' ... CType(TreeList1.Nodes(0), MyTreeListNode).Height = 25 ' ... End Sub End Class Public Class MyTreeList Inherits TreeList Protected Overrides Function CreateNode(ByVal nodeID As Integer, ByVal owner As TreeListNodes) As TreeListNode Return New MyTreeListNode(nodeID, owner) End Function Protected Overrides Sub InternalNodeChanged(ByVal node As TreeListNode, ByVal changeType As NodeChangeTypeEnum) If changeType = NodeChangeTypeEnum.User1 Then LayoutChanged() End If MyBase.InternalNodeChanged(node, changeType) End Sub Protected Overrides Sub RaiseCalcNodeHeight(ByVal node As TreeListNode, ByRef nodeHeight As Integer) Dim myNode As MyTreeListNode = CType(node, MyTreeListNode) If Not myNode Is Nothing Then nodeHeight = myNode.Height Else MyBase.RaiseCalcNodeHeight(node, nodeHeight) End If End Sub Public Overridable ReadOnly Property DefaultNodesHeight() As Integer Get Return 18 End Get End Property End Class Public Class MyTreeListNode Inherits TreeListNode Const minHeight As Integer = 5 Dim fHeight As Integer Public Sub New(ByVal id As Integer, ByVal owner As TreeListNodes) MyBase.New(id, owner) Me.fHeight = CType(owner.TreeList, MyTreeList).DefaultNodesHeight End Sub Public Property Height() As Integer Get Return fHeight End Get Set(ByVal Value As Integer) If fHeight = Value Or Value < minHeight Then Return fHeight = Value Changed(NodeChangeTypeEnum.User1) End Set End Property End Class |