123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- using System;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Reactive;
- using Avalonia.Controls;
- using Avalonia.Controls.Selection;
- using ControlCatalog.Pages;
- using MiniMvvm;
- namespace ControlCatalog.ViewModels
- {
- public class ListBoxPageViewModel : ViewModelBase
- {
- private bool _multiple;
- private bool _toggle;
- private bool _alwaysSelected;
- private bool _autoScrollToSelectedItem = true;
- private bool _wrapSelection;
- private int _counter;
- private IObservable<SelectionMode> _selectionMode;
- public ListBoxPageViewModel()
- {
- Items = new ObservableCollection<ItemModel>(Enumerable.Range(1, 10000).Select(i => GenerateItem()));
-
- Selection = new SelectionModel<ItemModel>();
- Selection.Select(1);
- _selectionMode = this.WhenAnyValue(
- x => x.Multiple,
- x => x.Toggle,
- x => x.AlwaysSelected,
- (m, t, a) =>
- (m ? Avalonia.Controls.SelectionMode.Multiple : 0) |
- (t ? Avalonia.Controls.SelectionMode.Toggle : 0) |
- (a ? Avalonia.Controls.SelectionMode.AlwaysSelected : 0));
- AddItemCommand = MiniCommand.Create(() => Items.Add(GenerateItem()));
- RemoveItemCommand = MiniCommand.Create(() =>
- {
- var items = Selection.SelectedItems.ToList();
- foreach (var item in items)
- {
- Items.Remove(item);
- }
- });
- SelectRandomItemCommand = MiniCommand.Create(() =>
- {
- var random = new Random();
- using (Selection.BatchUpdate())
- {
- Selection.Clear();
- Selection.Select(random.Next(Items.Count - 1));
- }
- });
- }
- public ObservableCollection<ItemModel> Items { get; }
- public SelectionModel<ItemModel> Selection { get; }
- public IObservable<SelectionMode> SelectionMode => _selectionMode;
- public bool Multiple
- {
- get => _multiple;
- set => this.RaiseAndSetIfChanged(ref _multiple, value);
- }
- public bool Toggle
- {
- get => _toggle;
- set => this.RaiseAndSetIfChanged(ref _toggle, value);
- }
- public bool AlwaysSelected
- {
- get => _alwaysSelected;
- set => this.RaiseAndSetIfChanged(ref _alwaysSelected, value);
- }
- public bool AutoScrollToSelectedItem
- {
- get => _autoScrollToSelectedItem;
- set => this.RaiseAndSetIfChanged(ref _autoScrollToSelectedItem, value);
- }
- public bool WrapSelection
- {
- get => _wrapSelection;
- set => this.RaiseAndSetIfChanged(ref _wrapSelection, value);
- }
- public MiniCommand AddItemCommand { get; }
- public MiniCommand RemoveItemCommand { get; }
- public MiniCommand SelectRandomItemCommand { get; }
- private ItemModel GenerateItem() => new ItemModel(_counter ++);
- }
- /// <summary>
- /// An Item model for the <see cref="ListBoxPage"/>
- /// </summary>
- public class ItemModel
- {
- /// <summary>
- /// Creates a new ItemModel with the given ID
- /// </summary>
- /// <param name="id">The ID to display</param>
- public ItemModel(int id)
- {
- ID = id;
- }
- /// <summary>
- /// The ID of this Item
- /// </summary>
- public int ID { get; }
- public override string ToString()
- {
- return $"Item {ID}";
- }
- }
- }
|