TreeViewPage.xaml.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System.Collections.ObjectModel;
  2. using System.Linq;
  3. using System.Reactive;
  4. using Avalonia.Controls;
  5. using Avalonia.Markup.Xaml;
  6. using ReactiveUI;
  7. namespace ControlCatalog.Pages
  8. {
  9. public class TreeViewPage : UserControl
  10. {
  11. public TreeViewPage()
  12. {
  13. InitializeComponent();
  14. DataContext = new PageViewModel();
  15. }
  16. private void InitializeComponent()
  17. {
  18. AvaloniaXamlLoader.Load(this);
  19. }
  20. private class PageViewModel : ReactiveObject
  21. {
  22. private SelectionMode _selectionMode;
  23. public PageViewModel()
  24. {
  25. Node root = new Node();
  26. Items = root.Children;
  27. Selection = new SelectionModel();
  28. AddItemCommand = ReactiveCommand.Create(() =>
  29. {
  30. Node parentItem = Selection.SelectedItems.Count > 0 ?
  31. (Node)Selection.SelectedItems[0] : root;
  32. parentItem.AddNewItem();
  33. });
  34. RemoveItemCommand = ReactiveCommand.Create(() =>
  35. {
  36. while (Selection.SelectedItems.Count > 0)
  37. {
  38. Node lastItem = (Node)Selection.SelectedItems[0];
  39. RecursiveRemove(Items, lastItem);
  40. Selection.DeselectAt(Selection.SelectedIndices[0]);
  41. }
  42. bool RecursiveRemove(ObservableCollection<Node> items, Node selectedItem)
  43. {
  44. if (items.Remove(selectedItem))
  45. {
  46. return true;
  47. }
  48. foreach (Node item in items)
  49. {
  50. if (item.AreChildrenInitialized && RecursiveRemove(item.Children, selectedItem))
  51. {
  52. return true;
  53. }
  54. }
  55. return false;
  56. }
  57. });
  58. }
  59. public ObservableCollection<Node> Items { get; }
  60. public SelectionModel Selection { get; }
  61. public ReactiveCommand<Unit, Unit> AddItemCommand { get; }
  62. public ReactiveCommand<Unit, Unit> RemoveItemCommand { get; }
  63. public SelectionMode SelectionMode
  64. {
  65. get => _selectionMode;
  66. set
  67. {
  68. Selection.ClearSelection();
  69. this.RaiseAndSetIfChanged(ref _selectionMode, value);
  70. }
  71. }
  72. }
  73. private class Node
  74. {
  75. private int _counter;
  76. private ObservableCollection<Node> _children;
  77. public string Header { get; private set; }
  78. public bool AreChildrenInitialized => _children != null;
  79. public ObservableCollection<Node> Children
  80. {
  81. get
  82. {
  83. if (_children == null)
  84. {
  85. _children = new ObservableCollection<Node>(Enumerable.Range(1, 10).Select(i => CreateNewNode()));
  86. }
  87. return _children;
  88. }
  89. }
  90. public void AddNewItem() => Children.Add(CreateNewNode());
  91. public override string ToString() => Header;
  92. private Node CreateNewNode() => new Node { Header = $"Item {_counter++}" };
  93. }
  94. }
  95. }