using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using Microsoft.Extensions.DependencyInjection; using SlnMesnac.Model.domain; using SlnMesnac.Repository.service; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; namespace SlnMesnac.WPF.ViewModel { public class SetKindWindowViewModel : ObservableObject { private ILogoFormulaService? formulaService; private ILogoConfigService? logoConfigService; private string CachMaterialType; // 缓存第一次查到的海康配方 public Dictionary TypeCache = new Dictionary(); public LogoConfig record = null; public delegate void SetKindWindowDelegate(); public SetKindWindowViewModel(string materialType) { formulaService = App.ServiceProvider.GetService(); logoConfigService = App.ServiceProvider.GetService(); SaveCommand = new RelayCommand(Save); // 初始加载内容 Init(materialType); } public void Init(string materialType) { record = logoConfigService.GetByMaterialType(materialType); if (record == null) return; MaterialType = record.MaterialName; IsDetectionChecked = record.IsChecked == 0 ? false : true; List list = formulaService.GetListAsync().Result; if(list!=null && list.Count > 0) { foreach (var item in list) { MaterialTypes.Add(item.Value); TypeCache.Add(item.Value, item.Key); } } } public void Save() { if (string.IsNullOrEmpty(_selectedMaterialType) && IsDetectionChecked) { MessageBox.Show("请选择型号"); return; } if (!string.IsNullOrEmpty(_selectedMaterialType)){ //需要检测 string select = _selectedMaterialType; TypeCache.TryGetValue(select, out int key); record.CheckKind = key; } #region 修改配方 if (record != null) { record.IsChecked = IsDetectionChecked ? 1 : 0; if(record.IsChecked == 0) { // 不检测,复位0 record.CheckKind = 0; } logoConfigService.updateByMaterialType(record); } #endregion MessageBox.Show("修改完成"); } #region 属性 /// /// 型号 /// private string _MaterialType; public string MaterialType { get { return _MaterialType; } set { _MaterialType = value; RaisePropertyChanged(); } } /// /// 单选框 /// private bool _IsDetectionChecked; public bool IsDetectionChecked { get { return _IsDetectionChecked; } set { _IsDetectionChecked = value; RaisePropertyChanged(); } } private string _selectedMaterialType; public string SelectedMaterialType { get { return _selectedMaterialType; } set { _selectedMaterialType = value; RaisePropertyChanged(); // 可以在这里处理选中项变化后的逻辑 } } /// /// 下拉框集合 /// private ObservableCollection _materialTypes = new ObservableCollection(); public ObservableCollection MaterialTypes { get { return _materialTypes; } set { _materialTypes = value; RaisePropertyChanged(); } } #endregion public RelayCommand SaveCommand { get; set; } } }