using BPASmartClient.FoodStationTest.Model; using BPASmartClient.Helper; using Microsoft.Toolkit.Mvvm.ComponentModel; using Microsoft.Toolkit.Mvvm.Input; using System.Collections.ObjectModel; using System.Linq; namespace BPASmartClient.FoodStationTest.ViewModel { internal class SelectRecipesViewModel : ObservableObject { public SelectRecipesViewModel() { AllRecipes = new(); foreach (var item in Json.Data.SelectedRecipes) { SelectRecipes.Add(item); } if (Json.Data.Recipes != null) { foreach (var item in Json.Data.Recipes) { AllRecipes.Add(new RecipeDataWithCheck() { Recipe = item, IsSelected = SelectRecipes.Any(p => p.RecipeCode == item.RecipeCode) }); } } ConfirmCommand = new(() => { RefreshSelected(); ActionManage.GetInstance.Send("CloseSelectRecipesView"); }); CancelCommand = new(() => { ActionManage.GetInstance.Send("CloseSelectRecipesView"); }); AllSelectCommand = new(() => { foreach (var item in AllRecipes) { item.IsSelected = true; } }); AllCancelCommand = new(() => { foreach (var item in AllRecipes) { item.IsSelected = false; } }); } private void RefreshSelected() { SelectRecipes.Clear(); foreach (var item in AllRecipes) { if (item.IsSelected == true) { SelectRecipes.Add(item.Recipe); } } } public ObservableCollection AllRecipes { get; set; } public static ObservableCollection SelectRecipes { get; set; } = new ObservableCollection(); /// /// 取消筛选,即关闭本窗口。 /// public RelayCommand CancelCommand { get; set; } /// /// 全选。 /// public RelayCommand AllSelectCommand { get; set; } /// /// 全部取消选择。 /// public RelayCommand AllCancelCommand { get; set; } /// /// 确认选择。 /// public RelayCommand ConfirmCommand { get; set; } internal class RecipeDataWithCheck : ObservableObject { public RemoteRecipeData Recipe { get; set; } private bool _IsSelected; public bool IsSelected { get { return _IsSelected; } set { _IsSelected = value; OnPropertyChanged(); } } } } }