TreeNode.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. /// Initialises a new instance with no <see cref="Text"/>
  29. /// </summary>
  30. public TreeNode () { }
  31. /// <summary>
  32. /// Initialises a new instance and sets starting <see cref="Text"/>
  33. /// </summary>
  34. public TreeNode (string text) => Text = text;
  35. /// <summary>
  36. /// Children of the current node
  37. /// </summary>
  38. /// <returns></returns>
  39. public virtual IList<ITreeNode> Children { get; set; } = new List<ITreeNode> ();
  40. /// <summary>
  41. /// Text to display in tree node for current entry
  42. /// </summary>
  43. /// <value></value>
  44. public virtual string Text { get; set; }
  45. /// <summary>
  46. /// Optionally allows you to store some custom data/class here.
  47. /// </summary>
  48. public object Tag { get; set; }
  49. /// <summary>
  50. /// returns <see cref="Text"/>
  51. /// </summary>
  52. /// <returns></returns>
  53. public override string ToString () => Text ?? "Unamed Node";
  54. }