Files
personal-toolbox/PersonalToolBox/ViewModels/CategoryEditViewModel.cs
home-PC dc94d17d99 Phase 3 缺陷修复 + 分类管理功能 + ComboBox 主题适配
缺陷修复: 日志自动滚动、StaticResource 顺序修复、ContextMenu PlacementTarget 绑定、PATH 命令支持、异常全量捕获、RefreshData 状态保留、全局崩溃日志

分类管理: 添加/编辑/删除分类,删除时工具移入全部,CategoryEditViewModel/Window

UI 修复: ComboBox 自定义模板,暗色模式弹出层/下拉项主题配色,文本绑定修复
2026-05-09 22:53:19 +08:00

81 lines
2.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using PersonalToolBox.Models;
using PersonalToolBox.Services;
namespace PersonalToolBox.ViewModels;
/// <summary>
/// 分类编辑窗口 ViewModel管理添加/编辑分类的交互逻辑
/// </summary>
public partial class CategoryEditViewModel : ObservableObject
{
private readonly IDataService _dataService;
private readonly ILogService _logService;
private readonly Category? _editingCategory;
[ObservableProperty]
private string _windowTitle = "添加分类";
[ObservableProperty]
private string _name = string.Empty;
public Action<bool?>? CloseAction { get; set; }
public bool Saved { get; private set; }
public CategoryEditViewModel(IDataService dataService, ILogService logService, Category? categoryToEdit = null)
{
_dataService = dataService;
_logService = logService;
_editingCategory = categoryToEdit;
if (categoryToEdit != null)
{
WindowTitle = "编辑分类";
Name = categoryToEdit.Name;
}
}
[RelayCommand]
private void Save()
{
try
{
if (string.IsNullOrWhiteSpace(Name))
{
_logService.Warning("分类名称不能为空");
return;
}
if (_editingCategory != null)
{
_editingCategory.Name = Name.Trim();
_logService.Info($"已更新分类: {Name.Trim()}");
}
else
{
_dataService.Config.Categories.Add(new Category
{
Id = Guid.NewGuid().ToString(),
Name = Name.Trim()
});
_logService.Info($"已添加分类: {Name.Trim()}");
}
_dataService.Save();
Saved = true;
CloseAction?.Invoke(true);
}
catch (Exception ex)
{
_logService.Error($"保存分类失败: {ex.Message}");
}
}
[RelayCommand]
private void Cancel()
{
CloseAction?.Invoke(false);
}
}