treeview.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.56.7.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. <meta property="docfx:navrel" content="../toc.html">
  16. <meta property="docfx:tocrel" content="../toc.html">
  17. <meta property="docfx:rel" content="../">
  18. </head>
  19. <body data-spy="scroll" data-target="#affix" data-offset="120">
  20. <div id="wrapper">
  21. <header>
  22. <nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation">
  23. <div class="container">
  24. <div class="navbar-header">
  25. <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
  26. <span class="sr-only">Toggle navigation</span>
  27. <span class="icon-bar"></span>
  28. <span class="icon-bar"></span>
  29. <span class="icon-bar"></span>
  30. </button>
  31. <a class="navbar-brand" href="../index.html">
  32. <img id="logo" class="svg" src="../images/logo48.png" alt="">
  33. </a>
  34. </div>
  35. <div class="collapse navbar-collapse" id="navbar">
  36. <form class="navbar-form navbar-right" role="search" id="search">
  37. <div class="form-group">
  38. <input type="text" class="form-control" id="search-query" placeholder="Search" autocomplete="off">
  39. </div>
  40. </form>
  41. </div>
  42. </div>
  43. </nav>
  44. <div class="subnav navbar navbar-default">
  45. <div class="container hide-when-search" id="breadcrumb">
  46. <ul class="breadcrumb">
  47. <li></li>
  48. </ul>
  49. </div>
  50. </div>
  51. </header>
  52. <div class="container body-content">
  53. <div id="search-results">
  54. <div class="search-list">Search Results for <span></span></div>
  55. <div class="sr-items">
  56. <p><i class="glyphicon glyphicon-refresh index-loading"></i></p>
  57. </div>
  58. <ul id="pagination" data-first="First" data-prev="Previous" data-next="Next" data-last="Last"></ul>
  59. </div>
  60. </div>
  61. <div role="main" class="container body-content hide-when-search">
  62. <div class="article row grid">
  63. <div class="col-md-10">
  64. <article class="content wrap" id="_content" data-uid="">
  65. <h1 id="tree-view">Tree View</h1>
  66. <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>
  67. <p><a href="api/Terminal.Gui/Terminal.Gui.TreeView.html">TreeView API Reference</a></p>
  68. <h2 id="using-treeview">Using TreeView</h2>
  69. <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>
  70. <pre><code class="lang-csharp">var tree = new TreeView()
  71. {
  72. X = 0,
  73. Y = 0,
  74. Width = 40,
  75. Height = 20
  76. };
  77. var root1 = new TreeNode(&quot;Root1&quot;);
  78. root1.Children.Add(new TreeNode(&quot;Child1.1&quot;));
  79. root1.Children.Add(new TreeNode(&quot;Child1.2&quot;));
  80. var root2 = new TreeNode(&quot;Root2&quot;);
  81. root2.Children.Add(new TreeNode(&quot;Child2.1&quot;));
  82. root2.Children.Add(new TreeNode(&quot;Child2.2&quot;));
  83. tree.AddObject(root1);
  84. tree.AddObject(root2);
  85. </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>
  86. <pre><code class="lang-csharp">// Your data class
  87. private class House : TreeNode {
  88. // Your properties
  89. public string Address {get;set;}
  90. public List&lt;Room&gt; Rooms {get;set;}
  91. // ITreeNode member:
  92. public override IList&lt;ITreeNode&gt; Children =&gt; Rooms.Cast&lt;ITreeNode&gt;().ToList();
  93. public override string Text { get =&gt; Address; set =&gt; Address = value; }
  94. }
  95. // Your other data class
  96. private class Room : TreeNode{
  97. public string Name {get;set;}
  98. public override string Text{get=&gt;Name;set{Name=value;}}
  99. }
  100. </code></pre><p>After implementing the interface you can add your objects directly to the tree</p>
  101. <pre><code class="lang-csharp">
  102. var myHouse = new House()
  103. {
  104. Address = &quot;23 Nowhere Street&quot;,
  105. Rooms = new List&lt;Room&gt;{
  106. new Room(){Name = &quot;Ballroom&quot;},
  107. new Room(){Name = &quot;Bedroom 1&quot;},
  108. new Room(){Name = &quot;Bedroom 2&quot;}
  109. }
  110. };
  111. var tree = new TreeView()
  112. {
  113. X = 0,
  114. Y = 0,
  115. Width = 40,
  116. Height = 20
  117. };
  118. tree.AddObject(myHouse);
  119. </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>
  120. <h2 id="treeviewt"><code>TreeView&lt;T&gt;</code></h2>
  121. <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>
  122. <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>
  123. <h3 id="implementing-itreebuildert"><code>Implementing ITreeBuilder&lt;T&gt;</code></h3>
  124. <p>Consider a simple data model that already exists in your program:</p>
  125. <pre><code class="lang-csharp">private abstract class GameObject
  126. {
  127. }
  128. private class Army : GameObject
  129. {
  130. public string Designation {get;set;}
  131. public List&lt;Unit&gt; Units {get;set;}
  132. public override string ToString ()
  133. {
  134. return Designation;
  135. }
  136. }
  137. private class Unit : GameObject
  138. {
  139. public string Name {get;set;}
  140. public override string ToString ()
  141. {
  142. return Name;
  143. }
  144. }
  145. </code></pre><p>An <code>ITreeBuilder&lt;T&gt;</code> for these classes might look like:</p>
  146. <pre><code class="lang-csharp">
  147. private class GameObjectTreeBuilder : ITreeBuilder&lt;GameObject&gt; {
  148. public bool SupportsCanExpand =&gt; true;
  149. public bool CanExpand (GameObject model)
  150. {
  151. return model is Army;
  152. }
  153. public IEnumerable&lt;GameObject&gt; GetChildren (GameObject model)
  154. {
  155. if(model is Army a)
  156. return a.Units;
  157. return Enumerable.Empty&lt;GameObject&gt;();
  158. }
  159. }
  160. </code></pre><p>To use the builder in a tree you would use:</p>
  161. <pre><code class="lang-csharp">var army1 = new Army()
  162. {
  163. Designation = &quot;3rd Infantry&quot;,
  164. Units = new List&lt;Unit&gt;{
  165. new Unit(){Name = &quot;Orc&quot;},
  166. new Unit(){Name = &quot;Troll&quot;},
  167. new Unit(){Name = &quot;Goblin&quot;},
  168. }
  169. };
  170. var tree = new TreeView&lt;GameObject&gt;()
  171. {
  172. X = 0,
  173. Y = 0,
  174. Width = 40,
  175. Height = 20,
  176. TreeBuilder = new GameObjectTreeBuilder()
  177. };
  178. tree.AddObject(army1);
  179. </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>
  180. <pre><code class="lang-csharp">tree.TreeBuilder = new DelegateTreeBuilder&lt;GameObject&gt;(
  181. (o)=&gt;o is Army a ? a.Units
  182. : Enumerable.Empty&lt;GameObject&gt;());
  183. </code></pre><h2 id="node-text-and-tostring">Node Text and ToString</h2>
  184. <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>
  185. <pre><code class="lang-csharp">treeViewFiles.AspectGetter = (f)=&gt;f.FullName;
  186. </code></pre></article>
  187. </div>
  188. <div class="hidden-sm col-md-2" role="complementary">
  189. <div class="sideaffix">
  190. <div class="contribution">
  191. <ul class="nav">
  192. </ul>
  193. </div>
  194. <nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix">
  195. <h5>In This Article</h5>
  196. <div></div>
  197. </nav>
  198. </div>
  199. </div>
  200. </div>
  201. </div>
  202. <footer>
  203. <div class="grad-bottom"></div>
  204. <div class="footer">
  205. <div class="container">
  206. <span class="pull-right">
  207. <a href="#top">Back to top</a>
  208. </span>
  209. <span>Generated by <strong>DocFX</strong></span>
  210. </div>
  211. </div>
  212. </footer>
  213. </div>
  214. <script type="text/javascript" src="../styles/docfx.vendor.js"></script>
  215. <script type="text/javascript" src="../styles/docfx.js"></script>
  216. <script type="text/javascript" src="../styles/main.js"></script>
  217. </body>
  218. </html>