DemoGroupViewModel.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. using System.Collections.ObjectModel;
  2. using CommunityToolkit.Mvvm.ComponentModel;
  3. using CommunityToolkit.Mvvm.Input;
  4. namespace WpfSample.ViewModels {
  5. internal partial class DemoGroupViewModel: ObservableObject {
  6. public DemoGroupViewModel() {
  7. Random random = new Random();
  8. for (int i = random.Next(1, 10); i >= 0; --i) {
  9. Items.Add(new DemoItemViewModel() {
  10. Name = $"I{i}",
  11. Value = $"V{random.Next()}",
  12. });
  13. }
  14. Index = random.Next(1, 109);
  15. }
  16. [ObservableProperty]
  17. private string name = string.Empty;
  18. public ObservableCollection<DemoItemViewModel> Items { get; } = new ObservableCollection<DemoItemViewModel>();
  19. public int Index { get; }
  20. [RelayCommand]
  21. private void AddItem() {
  22. Random random = new Random();
  23. Items.Add(new DemoItemViewModel() {
  24. Name = $"I{random.Next()}",
  25. Value = $"V{random.Next()}",
  26. });
  27. }
  28. }
  29. }