2024-09-22 11:05:41 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2025-02-10 20:53:40 +08:00
|
|
|
|
using System.Linq;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
using System.Text;
|
2025-02-10 20:53:40 +08:00
|
|
|
|
using System.Windows.Markup;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
|
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
2025-02-10 20:53:40 +08:00
|
|
|
|
using WPFluent;
|
|
|
|
|
|
using WPFluent.Controls;
|
|
|
|
|
|
using WPFluent.Extensions;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
namespace WpfApp;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
partial class MainViewModel : ObservableValidator
|
2024-09-22 11:05:41 +08:00
|
|
|
|
{
|
2025-02-10 20:53:40 +08:00
|
|
|
|
private ISnackbarService snackbarService;
|
|
|
|
|
|
private ControlAppearance snackbarAppearance = ControlAppearance.Danger;
|
|
|
|
|
|
private int snackbarTimeout = 5;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private SnackbarPresenter snackbarPresenter;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
private void ButtonClick()
|
|
|
|
|
|
{
|
|
|
|
|
|
snackbarService = new SnackbarService();
|
|
|
|
|
|
snackbarService.SetSnackbarPresenter(SnackbarPresenter);
|
|
|
|
|
|
snackbarService.Show(
|
|
|
|
|
|
"消息",
|
|
|
|
|
|
"Test",
|
|
|
|
|
|
snackbarAppearance,
|
|
|
|
|
|
new SymbolIcon(SymbolRegular.Fluent24),
|
|
|
|
|
|
TimeSpan.FromSeconds(snackbarTimeout)
|
|
|
|
|
|
);
|
|
|
|
|
|
//Toast.Information("I am information message");
|
|
|
|
|
|
//Toast.Error("I am error message");
|
|
|
|
|
|
//Toast.Success("I am success message");
|
|
|
|
|
|
//Toast.Warning("I am warning message");
|
|
|
|
|
|
//Toast.Show(owner: null!, "I am any message", new ToastConfig());
|
|
|
|
|
|
}
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
private void Loaded(object obj)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (obj is SnackbarPresenter snackbarPresenter)
|
2024-09-22 11:05:41 +08:00
|
|
|
|
{
|
2025-02-10 20:53:40 +08:00
|
|
|
|
this.SnackbarPresenter = snackbarPresenter;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
}
|
2025-02-10 20:53:40 +08:00
|
|
|
|
}
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
private ObservableCollection<string> animals =
|
|
|
|
|
|
[
|
|
|
|
|
|
"Cat",
|
|
|
|
|
|
"Dog",
|
|
|
|
|
|
"Bear",
|
|
|
|
|
|
"Lion",
|
|
|
|
|
|
"Mouse",
|
|
|
|
|
|
"Horse",
|
|
|
|
|
|
"Rat",
|
|
|
|
|
|
"Elephant",
|
|
|
|
|
|
"Kangaroo",
|
|
|
|
|
|
"Lizard",
|
|
|
|
|
|
"Snake",
|
|
|
|
|
|
"Frog",
|
|
|
|
|
|
"Fish",
|
|
|
|
|
|
"Butterfly",
|
|
|
|
|
|
"Human",
|
|
|
|
|
|
"Cow",
|
|
|
|
|
|
"Bumble Bee"
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
public ObservableCollection<Grade> Grades { get; set; } = [];
|
|
|
|
|
|
|
|
|
|
|
|
public ObservableCollection<Grade> SelectedItems { get; set; } = [];
|
|
|
|
|
|
|
|
|
|
|
|
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<string> Animals
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return animals; }
|
|
|
|
|
|
}
|
2024-10-27 00:19:48 +08:00
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
public MainViewModel()
|
|
|
|
|
|
{
|
|
|
|
|
|
GenerateTreeViewItem();
|
|
|
|
|
|
}
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
public List<MultiTreeViewItem> Items = [];
|
2024-10-27 00:19:48 +08:00
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
private string _selectedAnimal = "Cat";
|
|
|
|
|
|
public string SelectedAnimal
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return _selectedAnimal; }
|
|
|
|
|
|
set
|
2024-10-27 00:19:48 +08:00
|
|
|
|
{
|
2025-02-10 20:53:40 +08:00
|
|
|
|
_selectedAnimal = value;
|
|
|
|
|
|
OnPropertyChanged("SelectedAnimal");
|
2024-10-27 00:19:48 +08:00
|
|
|
|
}
|
2025-02-10 20:53:40 +08:00
|
|
|
|
}
|
2024-10-27 00:19:48 +08:00
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
private ObservableCollection<string> _selectedAnimals;
|
|
|
|
|
|
public ObservableCollection<string> SelectedAnimals
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
2024-10-27 00:19:48 +08:00
|
|
|
|
{
|
2025-02-10 20:53:40 +08:00
|
|
|
|
if (_selectedAnimals == null)
|
2024-10-27 00:19:48 +08:00
|
|
|
|
{
|
2025-02-10 20:53:40 +08:00
|
|
|
|
_selectedAnimals = ["Dog", "Lion", "Lizard"];
|
|
|
|
|
|
SelectedAnimalsText = WriteSelectedAnimalsString(_selectedAnimals);
|
|
|
|
|
|
_selectedAnimals.CollectionChanged += (s, e) =>
|
2024-10-27 00:19:48 +08:00
|
|
|
|
{
|
|
|
|
|
|
SelectedAnimalsText = WriteSelectedAnimalsString(_selectedAnimals);
|
2025-02-10 20:53:40 +08:00
|
|
|
|
OnPropertyChanged("SelectedAnimals");
|
|
|
|
|
|
};
|
2024-10-27 00:19:48 +08:00
|
|
|
|
}
|
2025-02-10 20:53:40 +08:00
|
|
|
|
return _selectedAnimals;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
}
|
2025-02-10 20:53:40 +08:00
|
|
|
|
set { _selectedAnimals = value; }
|
|
|
|
|
|
}
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
public string SelectedAnimalsText
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return _selectedAnimalsText; }
|
|
|
|
|
|
set
|
2024-10-27 00:19:48 +08:00
|
|
|
|
{
|
2025-02-10 20:53:40 +08:00
|
|
|
|
_selectedAnimalsText = value;
|
|
|
|
|
|
OnPropertyChanged("SelectedAnimalsText");
|
2024-10-27 00:19:48 +08:00
|
|
|
|
}
|
2025-02-10 20:53:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
string _selectedAnimalsText;
|
2024-10-27 00:19:48 +08:00
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
private static string WriteSelectedAnimalsString(IList<string> list)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (list.Count == 0)
|
|
|
|
|
|
return string.Empty;
|
2024-10-27 00:19:48 +08:00
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
var builder = new StringBuilder(list[0]);
|
2024-10-27 00:19:48 +08:00
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
for (var i = 1; i < list.Count; i++)
|
2024-10-27 00:19:48 +08:00
|
|
|
|
{
|
2025-02-10 20:53:40 +08:00
|
|
|
|
builder.Append(", ");
|
|
|
|
|
|
builder.Append(list[i]);
|
2024-10-27 00:19:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
return builder.ToString();
|
2024-10-27 00:19:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
[RelayCommand()]
|
|
|
|
|
|
private void ShowDialog()
|
2024-10-27 00:19:48 +08:00
|
|
|
|
{
|
2025-02-10 20:53:40 +08:00
|
|
|
|
MessageExtensions.ShowMessage();
|
2024-10-27 00:19:48 +08:00
|
|
|
|
}
|
2024-09-22 11:05:41 +08:00
|
|
|
|
}
|
2025-02-10 20:53:40 +08:00
|
|
|
|
|
|
|
|
|
|
class Grade
|
|
|
|
|
|
{
|
|
|
|
|
|
public string GradeLevel { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
public List<Student> Students { get; set; } = [];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Student
|
|
|
|
|
|
{
|
|
|
|
|
|
public int ID { get; set; }
|
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
|
public string Class { get; set; }
|
|
|
|
|
|
}
|
|
|
|
|
|
//class CustomerValidator : AbstractValidator<Student>
|
|
|
|
|
|
//{
|
|
|
|
|
|
// public CustomerValidator()
|
|
|
|
|
|
// {
|
|
|
|
|
|
// RuleFor(x => x.ID).NotEmpty();
|
|
|
|
|
|
// RuleFor(x => x.Name).NotEmpty().WithMessage("请指定姓名");
|
|
|
|
|
|
// RuleFor(x => x.Class).NotEmpty();
|
|
|
|
|
|
// }
|
|
|
|
|
|
//}
|