Files
Shrlalgo.RvKits/WpfApp/MainViewModel.cs

198 lines
5.0 KiB
C#
Raw Normal View History

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;
using System.Linq;
2024-09-22 11:05:41 +08:00
using System.Text;
using System.Windows.Markup;
2024-09-22 11:05:41 +08:00
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using WPFluent;
using WPFluent.Controls;
using WPFluent.Extensions;
2024-09-22 11:05:41 +08:00
namespace WpfApp;
2024-09-22 11:05:41 +08:00
partial class MainViewModel : ObservableValidator
2024-09-22 11:05:41 +08:00
{
private ISnackbarService snackbarService;
private ControlAppearance snackbarAppearance = ControlAppearance.Danger;
private int snackbarTimeout = 5;
2024-09-22 11:05:41 +08:00
[ObservableProperty]
private SnackbarPresenter snackbarPresenter;
2024-09-22 11:05:41 +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
[RelayCommand]
private void Loaded(object obj)
{
if (obj is SnackbarPresenter snackbarPresenter)
2024-09-22 11:05:41 +08:00
{
this.SnackbarPresenter = snackbarPresenter;
2024-09-22 11:05:41 +08:00
}
}
2024-09-22 11:05:41 +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; }
}
public MainViewModel()
{
GenerateTreeViewItem();
}
2024-09-22 11:05:41 +08:00
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;
2024-09-22 11:05:41 +08:00
}
set { _selectedAnimals = value; }
}
2024-09-22 11:05:41 +08:00
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();
}
2024-09-22 11:05:41 +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();
// }
//}