TreeNodeCollection.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.ComponentModel;
  6. using Avalonia.Collections;
  7. namespace Avalonia.Diagnostics.ViewModels
  8. {
  9. internal abstract class TreeNodeCollection : IAvaloniaReadOnlyList<TreeNode>, IDisposable
  10. {
  11. private AvaloniaList<TreeNode> _inner;
  12. public TreeNodeCollection(TreeNode owner) => Owner = owner;
  13. public TreeNode this[int index]
  14. {
  15. get
  16. {
  17. EnsureInitialized();
  18. return _inner[index];
  19. }
  20. }
  21. public int Count
  22. {
  23. get
  24. {
  25. EnsureInitialized();
  26. return _inner.Count;
  27. }
  28. }
  29. protected TreeNode Owner { get; }
  30. public event NotifyCollectionChangedEventHandler CollectionChanged
  31. {
  32. add => _inner.CollectionChanged += value;
  33. remove => _inner.CollectionChanged -= value;
  34. }
  35. public event PropertyChangedEventHandler PropertyChanged
  36. {
  37. add => _inner.PropertyChanged += value;
  38. remove => _inner.PropertyChanged -= value;
  39. }
  40. public virtual void Dispose()
  41. {
  42. if (_inner is object)
  43. {
  44. foreach (var node in _inner)
  45. {
  46. node.Dispose();
  47. }
  48. }
  49. }
  50. public IEnumerator<TreeNode> GetEnumerator()
  51. {
  52. EnsureInitialized();
  53. return _inner.GetEnumerator();
  54. }
  55. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  56. protected abstract void Initialize(AvaloniaList<TreeNode> nodes);
  57. private void EnsureInitialized()
  58. {
  59. if (_inner is null)
  60. {
  61. _inner = new AvaloniaList<TreeNode>();
  62. Initialize(_inner);
  63. }
  64. }
  65. }
  66. }