MainWindowViewModel.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reactive;
  5. using Avalonia.Collections;
  6. using Avalonia.Controls;
  7. using Avalonia.Controls.Primitives;
  8. using Avalonia.Layout;
  9. using Avalonia.Controls.Selection;
  10. using MiniMvvm;
  11. namespace VirtualizationDemo.ViewModels
  12. {
  13. internal class MainWindowViewModel : ViewModelBase
  14. {
  15. private int _itemCount = 200;
  16. private string _newItemString = "New Item";
  17. private int _newItemIndex;
  18. private AvaloniaList<ItemViewModel> _items;
  19. private string _prefix = "Item";
  20. private ScrollBarVisibility _horizontalScrollBarVisibility = ScrollBarVisibility.Auto;
  21. private ScrollBarVisibility _verticalScrollBarVisibility = ScrollBarVisibility.Auto;
  22. private Orientation _orientation = Orientation.Vertical;
  23. public MainWindowViewModel()
  24. {
  25. this.WhenAnyValue(x => x.ItemCount).Subscribe(ResizeItems);
  26. RecreateCommand = MiniCommand.Create(() => Recreate());
  27. AddItemCommand = MiniCommand.Create(() => AddItem());
  28. RemoveItemCommand = MiniCommand.Create(() => Remove());
  29. SelectFirstCommand = MiniCommand.Create(() => SelectItem(0));
  30. SelectLastCommand = MiniCommand.Create(() => SelectItem(Items.Count - 1));
  31. }
  32. public string NewItemString
  33. {
  34. get { return _newItemString; }
  35. set { this.RaiseAndSetIfChanged(ref _newItemString, value); }
  36. }
  37. public int ItemCount
  38. {
  39. get { return _itemCount; }
  40. set { this.RaiseAndSetIfChanged(ref _itemCount, value); }
  41. }
  42. public SelectionModel<ItemViewModel> Selection { get; } = new SelectionModel<ItemViewModel>();
  43. public AvaloniaList<ItemViewModel> Items
  44. {
  45. get { return _items; }
  46. private set { this.RaiseAndSetIfChanged(ref _items, value); }
  47. }
  48. public Orientation Orientation
  49. {
  50. get { return _orientation; }
  51. set { this.RaiseAndSetIfChanged(ref _orientation, value); }
  52. }
  53. public IEnumerable<Orientation> Orientations =>
  54. Enum.GetValues(typeof(Orientation)).Cast<Orientation>();
  55. public ScrollBarVisibility HorizontalScrollBarVisibility
  56. {
  57. get { return _horizontalScrollBarVisibility; }
  58. set { this.RaiseAndSetIfChanged(ref _horizontalScrollBarVisibility, value); }
  59. }
  60. public ScrollBarVisibility VerticalScrollBarVisibility
  61. {
  62. get { return _verticalScrollBarVisibility; }
  63. set { this.RaiseAndSetIfChanged(ref _verticalScrollBarVisibility, value); }
  64. }
  65. public IEnumerable<ScrollBarVisibility> ScrollBarVisibilities =>
  66. Enum.GetValues(typeof(ScrollBarVisibility)).Cast<ScrollBarVisibility>();
  67. public MiniCommand AddItemCommand { get; private set; }
  68. public MiniCommand RecreateCommand { get; private set; }
  69. public MiniCommand RemoveItemCommand { get; private set; }
  70. public MiniCommand SelectFirstCommand { get; private set; }
  71. public MiniCommand SelectLastCommand { get; private set; }
  72. public void RandomizeSize()
  73. {
  74. var random = new Random();
  75. foreach (var i in Items)
  76. {
  77. i.Height = random.Next(240) + 10;
  78. }
  79. }
  80. public void ResetSize()
  81. {
  82. foreach (var i in Items)
  83. {
  84. i.Height = double.NaN;
  85. }
  86. }
  87. private void ResizeItems(int count)
  88. {
  89. if (Items == null)
  90. {
  91. var items = Enumerable.Range(0, count)
  92. .Select(x => new ItemViewModel(x));
  93. Items = new AvaloniaList<ItemViewModel>(items);
  94. }
  95. else if (count > Items.Count)
  96. {
  97. var items = Enumerable.Range(Items.Count, count - Items.Count)
  98. .Select(x => new ItemViewModel(x));
  99. Items.AddRange(items);
  100. }
  101. else if (count < Items.Count)
  102. {
  103. Items.RemoveRange(count, Items.Count - count);
  104. }
  105. }
  106. private void AddItem()
  107. {
  108. var index = Items.Count;
  109. if (Selection.SelectedItems.Count > 0)
  110. {
  111. index = Selection.SelectedIndex;
  112. }
  113. Items.Insert(index, new ItemViewModel(_newItemIndex++, NewItemString));
  114. }
  115. private void Remove()
  116. {
  117. if (Selection.SelectedItems.Count > 0)
  118. {
  119. Items.RemoveAll(Selection.SelectedItems.ToList());
  120. }
  121. }
  122. private void Recreate()
  123. {
  124. _prefix = _prefix == "Item" ? "Recreated" : "Item";
  125. var items = Enumerable.Range(0, _itemCount)
  126. .Select(x => new ItemViewModel(x, _prefix));
  127. Items = new AvaloniaList<ItemViewModel>(items);
  128. }
  129. private void SelectItem(int index)
  130. {
  131. Selection.SelectedIndex = index;
  132. }
  133. }
  134. }