1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using System.Collections.ObjectModel;
namespace WpfApp1 { public partial class MainWindowViewModel : ObservableObject { [ObservableProperty] private ObservableCollection<Student> _students = new ObservableCollection<Student>();
[ObservableProperty] private Student _selectedStudent = null;
[RelayCommand] private void InsertLine(Object paramater) { var newStudent = new Student() { Name = "XXX", Motto = "XXX" }; Students.Insert(Students.IndexOf(SelectedStudent), newStudent); SelectedStudent = newStudent; }
[RelayCommand] private void DeleteLine(Object paramater) { var res = Students.Remove(SelectedStudent); SelectedStudent = null; } [RelayCommand] private void Paste() { MessageBox.Show("Paste"); }
public MainWindowViewModel() { Students.Clear(); List<Hobby> hobbies = new List<Hobby>() { new Hobby(){Name="跑步",Frequency=3,Address="江滩"}, new Hobby(){Name="散步",Frequency=5,Address="江滩"}, }; List<Hobby> hobbies2 = new List<Hobby>() { new Hobby(){Name="瑜伽",Frequency=3,Address="江滩"}, new Hobby(){Name="健身",Frequency=5,Address="江滩"}, };
Students.Clear(); Students.Add(new Student(1, "小红", 32, "Hello World 1!") { Hobbys = new ObservableCollection<Hobby>(hobbies), }); Students.Add(new Student(2, "小宇", 32, "Hello World 2!") { Hobbys = new ObservableCollection<Hobby>(hobbies), }); Students.Add(new Student(3, "小明", 32, "Hello World 3!") { Hobbys = new ObservableCollection<Hobby>(hobbies2), }); Students.Add(new Student(4, "小亮", 32, "Hello World 4!") { Hobbys = new ObservableCollection<Hobby>(hobbies2), }); } }
public partial class Student : ObservableObject { public Student() { }
public Student(int id, string name, int age, string motto) { Id = id; Name = name; Age = age; Motto = motto; }
[ObservableProperty] private int _id; [ObservableProperty] private string _name; [ObservableProperty] private int _age; [ObservableProperty] private string _motto; [ObservableProperty] private ObservableCollection<Hobby> _hobbys = new ObservableCollection<Hobby>(); }
public partial class Hobby : ObservableObject { [ObservableProperty] private string _name;
[ObservableProperty] private int _frequency;
[ObservableProperty] private string _address; } }
|