using PDF_Office.Properties; using PDFSettings; using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace PDF_Office.ViewModels.EditTools.Background { public class BackgroundTemplateListFileContentViewModel : BindableBase,INavigationAware { public ObservableCollection backgroundModFileCollection = new ObservableCollection(); public ObservableCollection BackgroundModFileCollection { get { return backgroundModFileCollection; } set { backgroundModFileCollection = value; RaisePropertyChanged(); } } private Visibility _createTemplateVisible; public Visibility CreateTemplateVisible { get { return _createTemplateVisible; } set { SetProperty(ref _createTemplateVisible, value); } } public DelegateCommand AddTemplateCommand { get; set; } public DelegateCommand DeleteTemplateItemCommand { get; set; } BackgroundTemplateListFileContentViewModel() { AddTemplateCommand = new DelegateCommand(AddTemplate); DeleteTemplateItemCommand = new DelegateCommand(DeleteTemplateItem); InitBackgroundTemplateList(); } private void CheckTemplateListIsEmpty(List backgroundTemplateList) { if (backgroundTemplateList.Count() == 0) { CreateTemplateVisible = Visibility.Visible; } else { CreateTemplateVisible = Visibility.Collapsed; } } private void GetBackgroundSource() { List backgroundModFileTemplateList = new List(); for (int temp = 0; temp < Settings.Default.BackgroundTemplateList.Count; temp++) { if (Settings.Default.BackgroundTemplateList[temp].type == ComPDFKit.PDFDocument.C_Background_Type.BG_TYPE_IMAGE) { backgroundModFileTemplateList.Add(Settings.Default.BackgroundTemplateList[temp]); } } BackgroundModFileCollection = new ObservableCollection(backgroundModFileTemplateList); CheckTemplateListIsEmpty(backgroundModFileTemplateList); } private void InitBackgroundTemplateList() { if (Settings.Default.BackgroundTemplateList == null) { Settings.Default.BackgroundTemplateList = new BackgroundTemplateList(); } GetBackgroundSource(); } private void UpdateBackgroundSources() { int count = backgroundModFileCollection.Count + backgroundModFileCollection.Count; List backgroundModFileTemplateList = new List(); for (int temp = 0; temp < Settings.Default.BackgroundTemplateList.Count; temp++) { if (Settings.Default.BackgroundTemplateList[temp].type == ComPDFKit.PDFDocument.C_Background_Type.BG_TYPE_IMAGE) { backgroundModFileTemplateList.Add(Settings.Default.BackgroundTemplateList[temp]); } } BackgroundModFileCollection = new ObservableCollection(backgroundModFileTemplateList); CheckTemplateListIsEmpty(backgroundModFileTemplateList); } public void AddTemplate() { var backgroundItem = new BackgroundItem(); backgroundItem.pageRange = "0"; backgroundItem.type = ComPDFKit.PDFDocument.C_Background_Type.BG_TYPE_IMAGE; backgroundItem.templateName += Settings.Default.BackgroundIndex.ToString(); Settings.Default.BackgroundTemplateList.Add(backgroundItem); Settings.Default.Save(); UpdateBackgroundSources(); } public void DeleteTemplateItem(object e) { var btn = e as System.Windows.Controls.Button; if (btn == null) { return; } var template = btn.DataContext as BackgroundItem; if (template == null) { return; } Settings.Default.AppProperties.NeedToDeletePath.Add(template.previeImagePath); Settings.Default.AppProperties.NeedToDeletePath.Add(template.imagepath); Settings.Default.BackgroundTemplateList.Remove(template); Settings.Default.Save(); BackgroundModFileCollection.Remove(template); UpdateBackgroundSources(); } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { } public void OnNavigatedTo(NavigationContext navigationContext) { } } }