ListBoxPageViewModel.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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(() => Items.Add(GenerateItem()));
  33. RemoveItemCommand = MiniCommand.Create(() =>
  34. {
  35. var items = Selection.SelectedItems.ToList();
  36. foreach (var item in items)
  37. {
  38. Items.Remove(item);
  39. }
  40. });
  41. SelectRandomItemCommand = MiniCommand.Create(() =>
  42. {
  43. var random = new Random();
  44. using (Selection.BatchUpdate())
  45. {
  46. Selection.Clear();
  47. Selection.Select(random.Next(Items.Count - 1));
  48. }
  49. });
  50. }
  51. public ObservableCollection<ItemModel> Items { get; }
  52. public SelectionModel<ItemModel> Selection { get; }
  53. public IObservable<SelectionMode> SelectionMode => _selectionMode;
  54. public bool Multiple
  55. {
  56. get => _multiple;
  57. set => this.RaiseAndSetIfChanged(ref _multiple, value);
  58. }
  59. public bool Toggle
  60. {
  61. get => _toggle;
  62. set => this.RaiseAndSetIfChanged(ref _toggle, value);
  63. }
  64. public bool AlwaysSelected
  65. {
  66. get => _alwaysSelected;
  67. set => this.RaiseAndSetIfChanged(ref _alwaysSelected, value);
  68. }
  69. public bool AutoScrollToSelectedItem
  70. {
  71. get => _autoScrollToSelectedItem;
  72. set => this.RaiseAndSetIfChanged(ref _autoScrollToSelectedItem, value);
  73. }
  74. public bool WrapSelection
  75. {
  76. get => _wrapSelection;
  77. set => this.RaiseAndSetIfChanged(ref _wrapSelection, value);
  78. }
  79. public MiniCommand AddItemCommand { get; }
  80. public MiniCommand RemoveItemCommand { get; }
  81. public MiniCommand SelectRandomItemCommand { get; }
  82. private ItemModel GenerateItem() => new ItemModel(_counter ++);
  83. }
  84. /// <summary>
  85. /// An Item model for the <see cref="ListBoxPage"/>
  86. /// </summary>
  87. public class ItemModel
  88. {
  89. /// <summary>
  90. /// Creates a new ItemModel with the given ID
  91. /// </summary>
  92. /// <param name="id">The ID to display</param>
  93. public ItemModel(int id)
  94. {
  95. ID = id;
  96. }
  97. /// <summary>
  98. /// The ID of this Item
  99. /// </summary>
  100. public int ID { get; }
  101. public override string ToString()
  102. {
  103. return $"Item {ID}";
  104. }
  105. }
  106. }