TreeNode.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System.Collections.Generic;
  2. namespace Terminal.Gui {
  3. /// <summary>
  4. /// Interface to implement when you want the regular (non generic) <see cref="TreeView"/>
  5. /// to automatically determine children for your class (without having to specify
  6. /// an <see cref="ITreeBuilder{T}"/>)
  7. /// </summary>
  8. public interface ITreeNode {
  9. /// <summary>
  10. /// Text to display when rendering the node
  11. /// </summary>
  12. string Text { get; set; }
  13. /// <summary>
  14. /// The children of your class which should be rendered underneath it when expanded
  15. /// </summary>
  16. /// <value></value>
  17. IList<ITreeNode> Children { get; }
  18. /// <summary>
  19. /// Optionally allows you to store some custom data/class here.
  20. /// </summary>
  21. object Tag { get; set; }
  22. }
  23. /// <summary>
  24. /// Simple class for representing nodes, use with regular (non generic) <see cref="TreeView"/>.
  25. /// </summary>
  26. public class TreeNode : ITreeNode {
  27. /// <summary>
  28. /// Children of the current node
  29. /// </summary>
  30. /// <returns></returns>
  31. public virtual IList<ITreeNode> Children { get; set; } = new List<ITreeNode> ();
  32. /// <summary>
  33. /// Text to display in tree node for current entry
  34. /// </summary>
  35. /// <value></value>
  36. public virtual string Text { get; set; }
  37. /// <summary>
  38. /// Optionally allows you to store some custom data/class here.
  39. /// </summary>
  40. public object Tag { get; set; }
  41. /// <summary>
  42. /// returns <see cref="Text"/>
  43. /// </summary>
  44. /// <returns></returns>
  45. public override string ToString ()
  46. {
  47. return Text ?? "Unamed Node";
  48. }
  49. /// <summary>
  50. /// Initialises a new instance with no <see cref="Text"/>
  51. /// </summary>
  52. public TreeNode ()
  53. {
  54. }
  55. /// <summary>
  56. /// Initialises a new instance and sets starting <see cref="Text"/>
  57. /// </summary>
  58. public TreeNode (string text)
  59. {
  60. Text = text;
  61. }
  62. }
  63. }