MainWindowViewModel.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.Collections.Generic;
  5. using System.Linq;
  6. using Avalonia.Collections;
  7. using Avalonia.Controls;
  8. using ReactiveUI;
  9. namespace VirtualizationTest.ViewModels
  10. {
  11. internal class MainWindowViewModel : ReactiveObject
  12. {
  13. private int _itemCount = 200;
  14. private string _newItemString = "New Item";
  15. private int _newItemIndex;
  16. private IReactiveList<ItemViewModel> _items;
  17. private string _prefix = "Item";
  18. private Orientation _orientation;
  19. private ItemVirtualizationMode _virtualizationMode = ItemVirtualizationMode.Simple;
  20. public MainWindowViewModel()
  21. {
  22. this.WhenAnyValue(x => x.ItemCount).Subscribe(ResizeItems);
  23. RecreateCommand = ReactiveCommand.Create();
  24. RecreateCommand.Subscribe(_ => Recreate());
  25. AddItemCommand = ReactiveCommand.Create();
  26. AddItemCommand.Subscribe(_ => AddItem());
  27. RemoveItemCommand = ReactiveCommand.Create();
  28. RemoveItemCommand.Subscribe(_ => Remove());
  29. SelectFirstCommand = ReactiveCommand.Create();
  30. SelectFirstCommand.Subscribe(_ => SelectItem(0));
  31. SelectLastCommand = ReactiveCommand.Create();
  32. SelectLastCommand.Subscribe(_ => SelectItem(Items.Count - 1));
  33. }
  34. public string NewItemString
  35. {
  36. get { return _newItemString; }
  37. set { this.RaiseAndSetIfChanged(ref _newItemString, value); }
  38. }
  39. public int ItemCount
  40. {
  41. get { return _itemCount; }
  42. set { this.RaiseAndSetIfChanged(ref _itemCount, value); }
  43. }
  44. public AvaloniaList<ItemViewModel> SelectedItems { get; }
  45. = new AvaloniaList<ItemViewModel>();
  46. public IReactiveList<ItemViewModel> Items
  47. {
  48. get { return _items; }
  49. private set { this.RaiseAndSetIfChanged(ref _items, value); }
  50. }
  51. public Orientation Orientation
  52. {
  53. get { return _orientation; }
  54. set { this.RaiseAndSetIfChanged(ref _orientation, value); }
  55. }
  56. public IEnumerable<Orientation> Orientations =>
  57. Enum.GetValues(typeof(Orientation)).Cast<Orientation>();
  58. public ItemVirtualizationMode VirtualizationMode
  59. {
  60. get { return _virtualizationMode; }
  61. set { this.RaiseAndSetIfChanged(ref _virtualizationMode, value); }
  62. }
  63. public IEnumerable<ItemVirtualizationMode> VirtualizationModes =>
  64. Enum.GetValues(typeof(ItemVirtualizationMode)).Cast<ItemVirtualizationMode>();
  65. public ReactiveCommand<object> AddItemCommand { get; private set; }
  66. public ReactiveCommand<object> RecreateCommand { get; private set; }
  67. public ReactiveCommand<object> RemoveItemCommand { get; private set; }
  68. public ReactiveCommand<object> SelectFirstCommand { get; private set; }
  69. public ReactiveCommand<object> SelectLastCommand { get; private set; }
  70. private void ResizeItems(int count)
  71. {
  72. if (Items == null)
  73. {
  74. var items = Enumerable.Range(0, count)
  75. .Select(x => new ItemViewModel(x));
  76. Items = new ReactiveList<ItemViewModel>(items);
  77. }
  78. else if (count > Items.Count)
  79. {
  80. var items = Enumerable.Range(Items.Count, count - Items.Count)
  81. .Select(x => new ItemViewModel(x));
  82. Items.AddRange(items);
  83. }
  84. else if (count < Items.Count)
  85. {
  86. Items.RemoveRange(count, Items.Count - count);
  87. }
  88. }
  89. private void AddItem()
  90. {
  91. var index = Items.Count;
  92. if (SelectedItems.Count > 0)
  93. {
  94. index = Items.IndexOf(SelectedItems[0]);
  95. }
  96. Items.Insert(index, new ItemViewModel(_newItemIndex++, NewItemString));
  97. }
  98. private void Remove()
  99. {
  100. if (SelectedItems.Count > 0)
  101. {
  102. Items.RemoveAll(SelectedItems);
  103. }
  104. }
  105. private void Recreate()
  106. {
  107. _prefix = _prefix == "Item" ? "Recreated" : "Item";
  108. var items = Enumerable.Range(0, _itemCount)
  109. .Select(x => new ItemViewModel(x, _prefix));
  110. Items = new ReactiveList<ItemViewModel>(items);
  111. }
  112. private void SelectItem(int index)
  113. {
  114. SelectedItems.Clear();
  115. SelectedItems.Add(Items[index]);
  116. }
  117. }
  118. }