using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Text; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Sai.RvKits; using Wpf.Ui.Controls; using Wpf.Ui; using Wpf.Ui.Extend.Controls; namespace WpfApp { partial class MainViewModel : ObservableValidator { private ISnackbarService snackbarService; private ControlAppearance snackbarAppearance = ControlAppearance.Danger; private int snackbarTimeout = 5; [ObservableProperty] private SnackbarPresenter snackbarPresenter; [RelayCommand] private void ButtonClick() { snackbarService = new SnackbarService(); snackbarService.SetSnackbarPresenter(SnackbarPresenter); snackbarService.Show( "消息", "Test", snackbarAppearance, new SymbolIcon(SymbolRegular.Fluent24), TimeSpan.FromSeconds(snackbarTimeout) ); } [RelayCommand] private void Loaded(object obj) { if (obj is SnackbarPresenter snackbarPresenter) { this.SnackbarPresenter = snackbarPresenter; } } private ObservableCollection animals = new() { "Cat", "Dog", "Bear", "Lion", "Mouse", "Horse", "Rat", "Elephant", "Kangaroo", "Lizard", "Snake", "Frog", "Fish", "Butterfly", "Human", "Cow", "Bumble Bee" }; public ObservableCollection Grades { get; set; } = new(); public ObservableCollection SelectedItems { get; set; } = new(); public void GenerateTreeViewItem() { Grade item = new() { GradeLevel = "Grade1" }; item.Students.Add(new() { Name = "St1" }); item.Students.Add(new() { Name = "St2" }); item.Students.Add(new() { Name = "St3" }); Grade item1 = new() { GradeLevel = "Grade2" }; item1.Students.Add(new() { Name = "StA" }); item1.Students.Add(new() { Name = "StB" }); Grades.Add(item); Grades.Add(item1); } [ObservableProperty] [NotifyDataErrorInfo] [MinLength(1)] [Range(10,1000,ErrorMessage ="请输入大于10,小于1000的值")] [Required(ErrorMessage ="必填")] private string number; public ObservableCollection Animals { get { return animals; } } public MainViewModel() { GenerateTreeViewItem(); } public List Items = new(); private string _selectedAnimal = "Cat"; public string SelectedAnimal { get { return _selectedAnimal; } set { _selectedAnimal = value; OnPropertyChanged("SelectedAnimal"); } } private ObservableCollection _selectedAnimals; public ObservableCollection SelectedAnimals { get { if (_selectedAnimals == null) { _selectedAnimals = new ObservableCollection { "Dog", "Lion", "Lizard" }; SelectedAnimalsText = WriteSelectedAnimalsString(_selectedAnimals); _selectedAnimals.CollectionChanged += (s, e) => { SelectedAnimalsText = WriteSelectedAnimalsString(_selectedAnimals); OnPropertyChanged("SelectedAnimals"); }; } return _selectedAnimals; } set { _selectedAnimals = value; } } public string SelectedAnimalsText { get { return _selectedAnimalsText; } set { _selectedAnimalsText = value; OnPropertyChanged("SelectedAnimalsText"); } } string _selectedAnimalsText; private static string WriteSelectedAnimalsString(IList list) { if (list.Count == 0) return String.Empty; var builder = new StringBuilder(list[0]); for (var i = 1; i < list.Count; i++) { builder.Append(", "); builder.Append(list[i]); } return builder.ToString(); } [RelayCommand()] private void ShowDialog() { MessageExtensions.ShowMessage(); } } class Grade { public string GradeLevel { get; set; } public List Students { get; set; } = new List(); } class Student { public int ID { get; set; } public string Name { get; set; } public string Class { get; set; } } //class CustomerValidator : AbstractValidator //{ // public CustomerValidator() // { // RuleFor(x => x.ID).NotEmpty(); // RuleFor(x => x.Name).NotEmpty().WithMessage("请指定姓名"); // RuleFor(x => x.Class).NotEmpty(); // } //} }