ListBoxPageViewModel.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Linq;
  4. using Avalonia.Controls;
  5. using Avalonia.Controls.Selection;
  6. using ControlCatalog.Pages;
  7. using MiniMvvm;
  8. namespace ControlCatalog.ViewModels
  9. {
  10. public class ListBoxPageViewModel : ViewModelBase
  11. {
  12. private bool _multiple;
  13. private bool _toggle;
  14. private bool _alwaysSelected;
  15. private bool _autoScrollToSelectedItem = true;
  16. private bool _wrapSelection;
  17. private int _counter;
  18. private IObservable<SelectionMode> _selectionMode;
  19. public ListBoxPageViewModel()
  20. {
  21. Items = new ObservableCollection<ItemModel>(Enumerable.Range(1, 10000).Select(i => GenerateItem()));
  22. Selection = new SelectionModel<ItemModel>();
  23. Selection.Select(1);
  24. _selectionMode = this.WhenAnyValue(
  25. x => x.Multiple,
  26. x => x.Toggle,
  27. x => x.AlwaysSelected,
  28. (m, t, a) =>
  29. (m ? Avalonia.Controls.SelectionMode.Multiple : 0) |
  30. (t ? Avalonia.Controls.SelectionMode.Toggle : 0) |
  31. (a ? Avalonia.Controls.SelectionMode.AlwaysSelected : 0));
  32. AddItemCommand = MiniCommand.Create(() =>
  33. {
  34. var item = GenerateItem();
  35. Items.Add(item);
  36. Selection.Clear();
  37. Selection.Select(Items.Count - 1);
  38. });
  39. RemoveItemCommand = MiniCommand.Create(() =>
  40. {
  41. var items = Selection.SelectedItems.ToList();
  42. foreach (var item in items)
  43. {
  44. Items.Remove(item!);
  45. }
  46. });
  47. SelectRandomItemCommand = MiniCommand.Create(() =>
  48. {
  49. var random = new Random();
  50. using (Selection.BatchUpdate())
  51. {
  52. Selection.Clear();
  53. Selection.Select(random.Next(Items.Count - 1));
  54. }
  55. });
  56. }
  57. public ObservableCollection<ItemModel> Items { get; }
  58. public SelectionModel<ItemModel> Selection { get; }
  59. public IObservable<SelectionMode> SelectionMode => _selectionMode;
  60. public bool Multiple
  61. {
  62. get => _multiple;
  63. set => this.RaiseAndSetIfChanged(ref _multiple, value);
  64. }
  65. public bool Toggle
  66. {
  67. get => _toggle;
  68. set => this.RaiseAndSetIfChanged(ref _toggle, value);
  69. }
  70. public bool AlwaysSelected
  71. {
  72. get => _alwaysSelected;
  73. set => this.RaiseAndSetIfChanged(ref _alwaysSelected, value);
  74. }
  75. public bool AutoScrollToSelectedItem
  76. {
  77. get => _autoScrollToSelectedItem;
  78. set => this.RaiseAndSetIfChanged(ref _autoScrollToSelectedItem, value);
  79. }
  80. public bool WrapSelection
  81. {
  82. get => _wrapSelection;
  83. set => this.RaiseAndSetIfChanged(ref _wrapSelection, value);
  84. }
  85. public MiniCommand AddItemCommand { get; }
  86. public MiniCommand RemoveItemCommand { get; }
  87. public MiniCommand SelectRandomItemCommand { get; }
  88. private ItemModel GenerateItem() => new ItemModel(_counter++);
  89. }
  90. /// <summary>
  91. /// An Item model for the <see cref="ListBoxPage"/>
  92. /// </summary>
  93. public class ItemModel
  94. {
  95. /// <summary>
  96. /// Creates a new ItemModel with the given ID
  97. /// </summary>
  98. /// <param name="id">The ID to display</param>
  99. public ItemModel(int id)
  100. {
  101. ID = id;
  102. }
  103. /// <summary>
  104. /// The ID of this Item
  105. /// </summary>
  106. public int ID { get; }
  107. public override string ToString()
  108. {
  109. return $"Item {ID}";
  110. }
  111. }
  112. }