1
0

MainWindowViewModel.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Linq;
  5. using Avalonia.Collections;
  6. using ReactiveUI;
  7. namespace VirtualizationTest.ViewModels
  8. {
  9. internal class MainWindowViewModel : ReactiveObject
  10. {
  11. private int _itemCount = 200;
  12. private string _newItemString = "New Item";
  13. private int _newItemIndex;
  14. private IReactiveList<ItemViewModel> _items;
  15. private string _prefix = "Item";
  16. public MainWindowViewModel()
  17. {
  18. this.WhenAnyValue(x => x.ItemCount).Subscribe(ResizeItems);
  19. RecreateCommand = ReactiveCommand.Create();
  20. RecreateCommand.Subscribe(_ => Recreate());
  21. AddItemCommand = ReactiveCommand.Create();
  22. AddItemCommand.Subscribe(_ => AddItem());
  23. RemoveItemCommand = ReactiveCommand.Create();
  24. RemoveItemCommand.Subscribe(_ => Remove());
  25. }
  26. public string NewItemString
  27. {
  28. get { return _newItemString; }
  29. set { this.RaiseAndSetIfChanged(ref _newItemString, value); }
  30. }
  31. public int ItemCount
  32. {
  33. get { return _itemCount; }
  34. set { this.RaiseAndSetIfChanged(ref _itemCount, value); }
  35. }
  36. public AvaloniaList<ItemViewModel> SelectedItems { get; }
  37. = new AvaloniaList<ItemViewModel>();
  38. public IReactiveList<ItemViewModel> Items
  39. {
  40. get { return _items; }
  41. private set { this.RaiseAndSetIfChanged(ref _items, value); }
  42. }
  43. public ReactiveCommand<object> AddItemCommand { get; private set; }
  44. public ReactiveCommand<object> RecreateCommand { get; private set; }
  45. public ReactiveCommand<object> RemoveItemCommand { get; private set; }
  46. private void ResizeItems(int count)
  47. {
  48. if (Items == null)
  49. {
  50. var items = Enumerable.Range(0, count)
  51. .Select(x => new ItemViewModel(x));
  52. Items = new ReactiveList<ItemViewModel>(items);
  53. }
  54. else if (count > Items.Count)
  55. {
  56. var items = Enumerable.Range(Items.Count, count - Items.Count)
  57. .Select(x => new ItemViewModel(x));
  58. Items.AddRange(items);
  59. }
  60. else if (count < Items.Count)
  61. {
  62. Items.RemoveRange(count, Items.Count - count);
  63. }
  64. }
  65. private void AddItem()
  66. {
  67. var index = Items.Count;
  68. if (SelectedItems.Count > 0)
  69. {
  70. index = Items.IndexOf(SelectedItems[0]);
  71. }
  72. Items.Insert(index, new ItemViewModel(_newItemIndex++, NewItemString));
  73. }
  74. private void Remove()
  75. {
  76. if (SelectedItems.Count > 0)
  77. {
  78. Items.RemoveAll(SelectedItems);
  79. }
  80. }
  81. private void Recreate()
  82. {
  83. _prefix = _prefix == "Item" ? "Recreated" : "Item";
  84. var items = Enumerable.Range(0, _itemCount)
  85. .Select(x => new ItemViewModel(x, _prefix));
  86. Items = new ReactiveList<ItemViewModel>(items);
  87. }
  88. }
  89. }