Posted By

strelaras on 03/07/13


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite



 / Published in: C#

  1. /**
  2.   RadTreeView
  4. */
  5. RadTreeNode Node1 = new RadTreeNode("Node1");
  6. Node1.Tag = 1234;
  7. Node1.BackColor = Color.Blue;
  8. RadTreeNode Node2 = new RadTreeNode("Node2");
  9. RadTreeNode Node3 = new RadTreeNode("Node3");
  10. RadTreeNode Node4 = new RadTreeNode("Node4");
  11. radTreeView1.Nodes.Add(Node1);
  12. radTreeView1.Nodes.Add(Node2);
  13. Node1.Nodes.Add(Node3);
  14. Node2.Nodes.Add(Node4);
  15. //Alternative methods for adding nodes
  16. //RadTreeNode Node1 = radTreeView1.Nodes.Add("Node1");
  17. //RadTreeNode Node2 = radTreeView1.Nodes.Add("Node2");
  18. //Node1.Nodes.Add("Node3");
  19. //Node2.Nodes.Add("Node4");
  21. private void RemoveNodes()
  22. {
  23. //Remove a single node
  24. radTreeView1.Nodes[0].Remove();
  25. // removes all nodes from TreeView
  26. radTreeView1.Nodes.Clear();
  27. }
  28. /* Reordering Nodes
  29.   Use the Insert() and Add() methods of the RadTreeView Nodes collection to reorder nodes programmatically.
  31.   Formatting Nodes
  32.   Similar to RadGridView, RadTreeView is virtualized. Its visual nodes (TreeNodeElement) are reused by the data nodes (RadTreeNode) which bring increased performance and optimized memory footprint. You can easily format node element by handling the NodeFormatting event. The arguments of this event return the visual Node (and the data Node that is currently assigned to it) that is currently formatted and which you can additionally style.
  34.   Example: show different images for the RadTreeView nodes depending on their logical state
  35.   NOTE 1: Please note that you should always provide an 'else' clause for each 'if' clause that you have in the implementation of the NodeFormatting event. Skipping this operation will lead to incorrect images applied to the inappropriate nodes.
  36.   NOTE 2: It is also imporant that we are accessing the project resources outside the NodeFormatting event handler. Accessing project resources is a time consuming operation and since NodeFormatting is fired for every visible node, you may experience performance issues if you try to access such resources in the event handler.
  37. */
  38. Bitmap folderOpen = SamplesCS.Properties.Resources.folder_open;
  39. Bitmap folderClose = SamplesCS.Properties.Resources.folder_close;
  40. Bitmap file = SamplesCS.Properties.Resources.file;
  41. void radTreeView1_NodeFormatting(object sender, TreeNodeFormattingEventArgs e)
  42. {
  43. if (e.Node.Nodes.Count > 0)
  44. {
  45. if (e.Node.Expanded)
  46. {
  47. e.NodeElement.ImageElement.Image = folderOpen;
  48. }
  49. else
  50. {
  51. e.NodeElement.ImageElement.Image = folderClose;
  52. }
  53. }
  54. else
  55. {
  56. e.NodeElement.ImageElement.Image = file;
  57. }
  58. }
  60. // Bring a Node into View: To scroll the control to a node use the BringIntoView() method of RadTreeView:
  61. RadTreeNode lastRootNode = radTreeView1.Nodes[radTreeView1.Nodes.Count - 1];
  62. radTreeView1.BringIntoView(lastRootNode.Nodes[lastRootNode.Nodes.Count - 1]);
  64. /* Editing Nodes
  65.   By default RadTreeView does not allow node editing. If the AllowEditing property is set to true, the user may select a node and press F2 to initiate editing. By default a text editor is invoked and allows the editing of the node label. When the edit process ends the entered value is assigned to the node Text property. If the user cancels editing by pressing Escape the value is not persisted. Editing can also be initiated and canceled programmatically.
  66.   • Use the BeginEdit() method to initiate editing on the selected node
  67.   • Use the EndEdit() method to conclude editing. EndEdit() takes a single boolean parameter "cancelEdit" that when true does not persist user changes.
  69.   For more information check online:
  70. */
  72. /* Filtering Nodes
  73.   RadTreeView supports filtering of its nodes according to their Text property. In order to apply a filter, you should set the Filter property of RadTreeView to the desired text value.
  74. */
  75. this.radTreeView1.Filter = "new";
  77. /* Custom Filtering
  78.   Custom filtering is a flexible mechanism for filtering RadTreeView nodes by using custom logic. It has a higher priority than the applied FilterDescriptors.
  79.   In order to apply custom logic for filtering, you have to create a Predicate. Here is an example of a Predicate which will return just the nodes which text is longer than one char:
  80. */
  81. private bool FilterNode(RadTreeNode node)
  82. {
  83. if (node.Text.Length > 1)
  84. {
  85. return true;
  86. }
  88. return false;
  89. }
  90. // To set the Predicate to RadTreeView, use the FilterPredicate property of the control:
  91. radTreeView1.TreeViewElement.FilterPredicate = FilterNode;
  92. // At the end, in order to apply the filter to the control, just set the Filter property to any string, which will invoke the filtering operation:
  93. this.radTreeView1.Filter = "Custom";
  95. /* Finding Nodes
  96.   When searching for node(s) you have two options that you can use - the Find method of RadTreeView which searches for a specific node by text or a predefined Predicate and returns the first node that matches the search criteria, or to use the FindNodes method of the control which also provides overloads to search by text or a Predicate and returns an array of nodes as a result.
  97.   The following example demonstrates how to search for a single node by its text and how to get all nodes whose Tag is not null by using a Predicate:
  98.  */
  99. RadTreeNode resultOfSearch = radTreeView1.Find("Child Node");
  101. Predicate<RadTreeNode> match = new Predicate<RadTreeNode>(delegate(RadTreeNode node)
  102. {
  103. return node.Tag != null ? true : false;
  104. });
  105. RadTreeNode[] result = radTreeView1.FindNodes(match);
  107. /* Performance
  108.   To improve performance in data loading and other long running operations use the RadTreeView BeginUpdate() and EndUpdate() methods. Place BeginUpdate() and EndUpdate() just before and after blocks of code that add, remove or reorder large numbers of nodes.
  109.   See the Load On Demand topic for an example ( ).
  110. */
  112. /* Enabling Drag and Drop
  113.   The key RadTreeView boolean properties enable Drag and Drop functionality: AllowDragDrop, and AllowDrop.
  114.   •AllowDragDrop enables the drag and drop functionality for a tree view control. Drag and drop functionality can also be used to reorder nodes.
  115.   •AllowDrop when true accepts data that the user drags onto it. This event is used to interact with drag and drop for standard Windows controls.
  117.   *** NOTE 1: If you want to allow Drag n Drop FROM RadTreeView to other control then ONLY the AllowDrop should be set to true, otherwise the feedback icon (DragDrop Effect icon) is not displayed correctly for the RadTreeView. Set AllowDragDrop to true if drag and drop is required for within the RadTreeView itself!
  119.   NOTE 2: Multiple nodes can be dragged if the RadTreeView AllowMultiSelect property is true.
  120. */
  121. // Allow drop from a RadTreeView to a RadTextBox
  122. radTreeView1.AllowDrop = true;
  123. this.radTextBox1.AllowDrop = true;
  124. // Initiate the drag/drop operation from RadTreeView (sending the whole RadTreeNode as info).
  125. // NOTE: we use the NodeMouseDown event to know which Node we're on!
  126. private void radTreeView1_NodeMouseDown(object sender, Telerik.WinControls.UI.RadTreeViewMouseEventArgs e) {
  127. this.radTreeView1.DoDragDrop(e.Node, DragDropEffects.Copy | DragDropEffects.Move);
  128. }

Report this snippet  

You need to login to post a comment.