MainViewModel.cs 910 B

123456789101112131415161718192021222324252627282930
  1. using System.Collections.ObjectModel;
  2. using CommunityToolkit.Mvvm.ComponentModel;
  3. using CommunityToolkit.Mvvm.Input;
  4. namespace WpfSample.ViewModels {
  5. internal partial class MainViewModel: ObservableObject {
  6. public MainViewModel() {
  7. Random random = new Random();
  8. for (int i = random.Next(1, 10); i >= 0; --i) {
  9. Groups.Add(new DemoGroupViewModel() {
  10. Name = $"G{i}",
  11. });
  12. }
  13. }
  14. [ObservableProperty]
  15. private string message = string.Empty;
  16. public ObservableCollection<DemoGroupViewModel> Groups { get; set; } = new ObservableCollection<DemoGroupViewModel>();
  17. class Test {
  18. public string Name { get; set; } = string.Empty;
  19. }
  20. [RelayCommand]
  21. private void AddItem() {
  22. Groups[0].AddItemCommand?.Execute(null);
  23. }
  24. }
  25. }