198 lines
5.0 KiB
C#
198 lines
5.0 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Collections.ObjectModel;
|
||
using System.ComponentModel;
|
||
using System.ComponentModel.DataAnnotations;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Windows.Markup;
|
||
|
||
using CommunityToolkit.Mvvm.ComponentModel;
|
||
using CommunityToolkit.Mvvm.Input;
|
||
using WPFluent;
|
||
using WPFluent.Controls;
|
||
using WPFluent.Extensions;
|
||
|
||
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)
|
||
);
|
||
//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());
|
||
}
|
||
|
||
[RelayCommand]
|
||
private void Loaded(object obj)
|
||
{
|
||
if (obj is SnackbarPresenter snackbarPresenter)
|
||
{
|
||
this.SnackbarPresenter = snackbarPresenter;
|
||
}
|
||
}
|
||
|
||
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; }
|
||
}
|
||
|
||
public MainViewModel()
|
||
{
|
||
GenerateTreeViewItem();
|
||
}
|
||
|
||
public List<MultiTreeViewItem> Items = [];
|
||
|
||
private string _selectedAnimal = "Cat";
|
||
public string SelectedAnimal
|
||
{
|
||
get { return _selectedAnimal; }
|
||
set
|
||
{
|
||
_selectedAnimal = value;
|
||
OnPropertyChanged("SelectedAnimal");
|
||
}
|
||
}
|
||
|
||
private ObservableCollection<string> _selectedAnimals;
|
||
public ObservableCollection<string> SelectedAnimals
|
||
{
|
||
get
|
||
{
|
||
if (_selectedAnimals == null)
|
||
{
|
||
_selectedAnimals = ["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<string> 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<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();
|
||
// }
|
||
//} |