using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Controls; namespace BPASmartClient.MilkWithTea.ViewModel { partial class RecipeConfigeViewModel:ObservableObject { public static ObservableCollection Materails { get; set; } = new ObservableCollection(); public Dictionary materialNames { get; set; } = new Dictionary(); [ObservableProperty] private string _name = String.Empty; [RelayCommand] private void AddMaterial() { Materails.Add(new LocalMaterail()); } [RelayCommand] private void Delete(object o) { if (o == null) return; if(o is ListBoxItem id) { Materails.Remove((LocalMaterail)id.DataContext); } } [RelayCommand] private void Save() { if(Name == String.Empty) { return; } if(Json.Data.localRecipes.FirstOrDefault(p=>p.RecipeName == Name)!= null) { return; } foreach(var materail in Materails) { materail.MaterialName = materialNames[materail.MaterialID]; } Json.Data.localRecipes.Add(new LocalRecipe { RecipeID = Guid.NewGuid().ToString(), RecipeName = Name, localMaterails = Materails, }); Json.Save(); ActionManage.GetInstance.Send("RecipeConfigeViewClose"); } public RecipeConfigeViewModel() { if(Json.Data.localMaterails.Count > 0) { foreach(var item in Json.Data.localMaterails) { if(item.MaterialID!=null&&item.MaterialName!=null) { materialNames.Add(item.MaterialID, item.MaterialName); } } } Materails.Clear(); } } }