using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; namespace DrillTools { /// /// 刀具详情窗口视图模型 /// public class ToolDetailViewModel : INotifyPropertyChanged { private ToolItem? _tool; /// /// 刀具对象 /// public ToolItem? Tool { get => _tool; set { if (SetProperty(ref _tool, value)) { OnPropertyChanged(nameof(WindowTitle)); OnPropertyChanged(nameof(HoleLocationsHeader)); OnPropertyChanged(nameof(FormattedHoleLocations)); OnPropertyChanged(nameof(IsMachineCodeTool)); } } } /// /// 窗口标题 /// public string WindowTitle => Tool != null ? $"刀具详情 - T{Tool.ToolNumber:D2}" : "刀具详情"; /// /// 孔位信息标题 /// public string HoleLocationsHeader => Tool != null ? $"孔位信息 (共{Tool.HoleLocations?.Count ?? 0}个)" : "孔位信息"; /// /// 格式化后的孔位信息 /// public string FormattedHoleLocations { get { if (Tool?.HoleLocations == null || Tool.HoleLocations.Count == 0) { return "无孔位数据"; } return string.Join(Environment.NewLine, Tool.HoleLocations); } } /// /// 是否为机台码刀具 /// public bool IsMachineCodeTool => Tool?.ToolType == ToolType.MachineCode; /// /// 构造函数 /// public ToolDetailViewModel() { } /// /// 使用指定的刀具初始化视图模型 /// /// 刀具对象 public ToolDetailViewModel(ToolItem tool) : this() { Tool = tool; } public event PropertyChangedEventHandler? PropertyChanged; protected bool SetProperty(ref T field, T value, [CallerMemberName] string? propertyName = null) { if (EqualityComparer.Default.Equals(field, value)) return false; field = value; OnPropertyChanged(propertyName); return true; } protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } }