treeview.html 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <!DOCTYPE html>
  2. <!--[if IE]><![endif]-->
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  7. <title>Tree View </title>
  8. <meta name="viewport" content="width=device-width">
  9. <meta name="title" content="Tree View ">
  10. <meta name="generator" content="docfx 2.59.4.0">
  11. <link rel="shortcut icon" href="../favicon.ico">
  12. <link rel="stylesheet" href="../styles/docfx.vendor.css">
  13. <link rel="stylesheet" href="../styles/docfx.css">
  14. <link rel="stylesheet" href="../styles/main.css">
  15. <link href="https://fonts.googleapis.com/css?family=Source Sans Pro" rel="stylesheet">
  16. <link href="https://fonts.googleapis.com/css?family=Source Code Pro" rel="stylesheet">
  17. <meta property="docfx:navrel" content="../toc.html">
  18. <meta property="docfx:tocrel" content="../toc.html">
  19. <meta property="docfx:rel" content="../">
  20. </head> <body data-spy="scroll" data-target="#affix" data-offset="120">
  21. <div id="wrapper">
  22. <header>
  23. <nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation">
  24. <div class="container">
  25. <div class="navbar-header">
  26. <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
  27. <span class="sr-only">Toggle navigation</span>
  28. <span class="icon-bar"></span>
  29. <span class="icon-bar"></span>
  30. <span class="icon-bar"></span>
  31. </button>
  32. <a class="navbar-brand" href="../index.html">
  33. <img id="logo" class="svg" src="../images/logo48.png" alt="">
  34. </a>
  35. </div>
  36. <div class="collapse navbar-collapse" id="navbar">
  37. <form class="navbar-form navbar-right" role="search" id="search">
  38. <div class="form-group">
  39. <input type="text" class="form-control" id="search-query" placeholder="Search" autocomplete="off">
  40. </div>
  41. </form>
  42. </div>
  43. </div>
  44. </nav>
  45. <div class="subnav navbar navbar-default">
  46. <div class="container hide-when-search" id="breadcrumb">
  47. <ul class="breadcrumb">
  48. <li></li>
  49. </ul>
  50. </div>
  51. </div>
  52. </header>
  53. <div class="container body-content">
  54. <div id="search-results">
  55. <div class="search-list">Search Results for <span></span></div>
  56. <div class="sr-items">
  57. <p><i class="glyphicon glyphicon-refresh index-loading"></i></p>
  58. </div>
  59. <ul id="pagination" data-first="First" data-prev="Previous" data-next="Next" data-last="Last"></ul>
  60. </div>
  61. </div>
  62. <div role="main" class="container body-content hide-when-search">
  63. <div class="article row grid">
  64. <div class="col-md-10">
  65. <article class="content wrap" id="_content" data-uid="">
  66. <h1 id="tree-view">Tree View</h1>
  67. <p>TreeView is a control for navigating hierarchical objects. It comes in two forms <code>TreeView</code> and <code>TreeView&lt;T&gt;</code>.</p>
  68. <p><a href="../api/Terminal.Gui/Terminal.Gui.TreeView.html">TreeView API Reference</a></p>
  69. <h2 id="using-treeview">Using TreeView</h2>
  70. <p>The basic non generic TreeView class is populated by <code>ITreeNode</code> objects. The simplest tree you can make would look something like:</p>
  71. <pre><code class="lang-csharp">var tree = new TreeView()
  72. {
  73. X = 0,
  74. Y = 0,
  75. Width = 40,
  76. Height = 20
  77. };
  78. var root1 = new TreeNode(&quot;Root1&quot;);
  79. root1.Children.Add(new TreeNode(&quot;Child1.1&quot;));
  80. root1.Children.Add(new TreeNode(&quot;Child1.2&quot;));
  81. var root2 = new TreeNode(&quot;Root2&quot;);
  82. root2.Children.Add(new TreeNode(&quot;Child2.1&quot;));
  83. root2.Children.Add(new TreeNode(&quot;Child2.2&quot;));
  84. tree.AddObject(root1);
  85. tree.AddObject(root2);
  86. </code></pre><p>Having to create a bunch of TreeNode objects can be a pain especially if you already have your own objects e.g. <code>House</code>, <code>Room</code> etc. There are two ways to use your own classes without having to create nodes manually. Firstly you can implement the <code>ITreeNode</code> interface:</p>
  87. <pre><code class="lang-csharp">// Your data class
  88. private class House : TreeNode {
  89. // Your properties
  90. public string Address {get;set;}
  91. public List&lt;Room&gt; Rooms {get;set;}
  92. // ITreeNode member:
  93. public override IList&lt;ITreeNode&gt; Children =&gt; Rooms.Cast&lt;ITreeNode&gt;().ToList();
  94. public override string Text { get =&gt; Address; set =&gt; Address = value; }
  95. }
  96. // Your other data class
  97. private class Room : TreeNode{
  98. public string Name {get;set;}
  99. public override string Text{get=&gt;Name;set{Name=value;}}
  100. }
  101. </code></pre><p>After implementing the interface you can add your objects directly to the tree</p>
  102. <pre><code class="lang-csharp">
  103. var myHouse = new House()
  104. {
  105. Address = &quot;23 Nowhere Street&quot;,
  106. Rooms = new List&lt;Room&gt;{
  107. new Room(){Name = &quot;Ballroom&quot;},
  108. new Room(){Name = &quot;Bedroom 1&quot;},
  109. new Room(){Name = &quot;Bedroom 2&quot;}
  110. }
  111. };
  112. var tree = new TreeView()
  113. {
  114. X = 0,
  115. Y = 0,
  116. Width = 40,
  117. Height = 20
  118. };
  119. tree.AddObject(myHouse);
  120. </code></pre><p>Alternatively you can simply tell the tree how the objects relate to one another by implementing <code>ITreeBuilder&lt;T&gt;</code>. This is a good option if you don&#39;t have control of the data objects you are working with.</p>
  121. <h2 id="treeviewt"><code>TreeView&lt;T&gt;</code></h2>
  122. <p>The generic <code>Treeview&lt;T&gt;</code> allows you to store any object hierarchy where nodes implement Type T. For example if you are working with <code>DirectoryInfo</code> and <code>FileInfo</code> objects then you could create a <code>TreeView&lt;FileSystemInfo&gt;</code>. If you don&#39;t have a shared interface/base class for all nodes you can still declare a <code>TreeView&lt;object&gt;</code>.</p>
  123. <p>In order to use <code>TreeView&lt;T&gt;</code> you need to tell the tree how objects relate to one another (who are children of who). To do this you must provide an <code>ITreeBuilder&lt;T&gt;</code>.</p>
  124. <h3 id="implementing-itreebuildert"><code>Implementing ITreeBuilder&lt;T&gt;</code></h3>
  125. <p>Consider a simple data model that already exists in your program:</p>
  126. <pre><code class="lang-csharp">private abstract class GameObject
  127. {
  128. }
  129. private class Army : GameObject
  130. {
  131. public string Designation {get;set;}
  132. public List&lt;Unit&gt; Units {get;set;}
  133. public override string ToString ()
  134. {
  135. return Designation;
  136. }
  137. }
  138. private class Unit : GameObject
  139. {
  140. public string Name {get;set;}
  141. public override string ToString ()
  142. {
  143. return Name;
  144. }
  145. }
  146. </code></pre><p>An <code>ITreeBuilder&lt;T&gt;</code> for these classes might look like:</p>
  147. <pre><code class="lang-csharp">
  148. private class GameObjectTreeBuilder : ITreeBuilder&lt;GameObject&gt; {
  149. public bool SupportsCanExpand =&gt; true;
  150. public bool CanExpand (GameObject model)
  151. {
  152. return model is Army;
  153. }
  154. public IEnumerable&lt;GameObject&gt; GetChildren (GameObject model)
  155. {
  156. if(model is Army a)
  157. return a.Units;
  158. return Enumerable.Empty&lt;GameObject&gt;();
  159. }
  160. }
  161. </code></pre><p>To use the builder in a tree you would use:</p>
  162. <pre><code class="lang-csharp">var army1 = new Army()
  163. {
  164. Designation = &quot;3rd Infantry&quot;,
  165. Units = new List&lt;Unit&gt;{
  166. new Unit(){Name = &quot;Orc&quot;},
  167. new Unit(){Name = &quot;Troll&quot;},
  168. new Unit(){Name = &quot;Goblin&quot;},
  169. }
  170. };
  171. var tree = new TreeView&lt;GameObject&gt;()
  172. {
  173. X = 0,
  174. Y = 0,
  175. Width = 40,
  176. Height = 20,
  177. TreeBuilder = new GameObjectTreeBuilder()
  178. };
  179. tree.AddObject(army1);
  180. </code></pre><p>Alternatively you can use <code>DelegateTreeBuilder&lt;T&gt;</code> instead of implementing your own <code>ITreeBuilder&lt;T&gt;</code>. For example:</p>
  181. <pre><code class="lang-csharp">tree.TreeBuilder = new DelegateTreeBuilder&lt;GameObject&gt;(
  182. (o)=&gt;o is Army a ? a.Units
  183. : Enumerable.Empty&lt;GameObject&gt;());
  184. </code></pre><h2 id="node-text-and-tostring">Node Text and ToString</h2>
  185. <p>The default behavior of TreeView is to use the <code>ToString</code> method on the objects for rendering. You can customise this by changing the <code>AspectGetter</code>. For example:</p>
  186. <pre><code class="lang-csharp">treeViewFiles.AspectGetter = (f)=&gt;f.FullName;
  187. </code></pre></article>
  188. </div>
  189. <div class="hidden-sm col-md-2" role="complementary">
  190. <div class="sideaffix">
  191. <div class="contribution">
  192. <ul class="nav">
  193. <li>
  194. <a href="https://github.com/gui-cs/Terminal.Gui/blob/develop/docfx/articles/treeview.md/#L1" class="contribution-link">Improve this Doc</a>
  195. </li>
  196. </ul>
  197. </div>
  198. <nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix">
  199. <h5>In This Article</h5>
  200. <div></div>
  201. </nav>
  202. </div>
  203. </div>
  204. </div>
  205. </div>
  206. <footer>
  207. <div class="grad-bottom"></div>
  208. <div class="footer">
  209. <div class="container">
  210. <span class="pull-right">
  211. <a href="#top">Back to top</a>
  212. </span>
  213. <span>Generated by <strong>DocFX</strong></span>
  214. </div>
  215. </div>
  216. </footer>
  217. </div>
  218. <script type="text/javascript" src="../styles/docfx.vendor.js"></script>
  219. <script type="text/javascript" src="../styles/docfx.js"></script>
  220. <script type="text/javascript" src="../styles/main.js"></script>
  221. </body>
  222. </html>