BackgroundTemplateListFileContentViewModel.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using PDF_Office.Properties;
  2. using PDFSettings;
  3. using Prism.Commands;
  4. using Prism.Mvvm;
  5. using Prism.Regions;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. namespace PDF_Office.ViewModels.EditTools.Background
  14. {
  15. public class BackgroundTemplateListFileContentViewModel : BindableBase,INavigationAware
  16. {
  17. public ObservableCollection<BackgroundItem> backgroundModFileCollection = new ObservableCollection<BackgroundItem>();
  18. public ObservableCollection<BackgroundItem> BackgroundModFileCollection
  19. {
  20. get { return backgroundModFileCollection; }
  21. set
  22. {
  23. backgroundModFileCollection = value;
  24. RaisePropertyChanged();
  25. }
  26. }
  27. private Visibility _createTemplateVisible;
  28. public Visibility CreateTemplateVisible
  29. {
  30. get { return _createTemplateVisible; }
  31. set { SetProperty(ref _createTemplateVisible, value); }
  32. }
  33. public DelegateCommand AddTemplateCommand { get; set; }
  34. public DelegateCommand<object> DeleteTemplateItemCommand { get; set; }
  35. BackgroundTemplateListFileContentViewModel()
  36. {
  37. AddTemplateCommand = new DelegateCommand(AddTemplate);
  38. DeleteTemplateItemCommand = new DelegateCommand<object>(DeleteTemplateItem);
  39. InitBackgroundTemplateList();
  40. }
  41. private void CheckTemplateListIsEmpty(List<BackgroundItem> backgroundTemplateList)
  42. {
  43. if (backgroundTemplateList.Count() == 0)
  44. {
  45. CreateTemplateVisible = Visibility.Visible;
  46. }
  47. else
  48. {
  49. CreateTemplateVisible = Visibility.Collapsed;
  50. }
  51. }
  52. private void GetBackgroundSource()
  53. {
  54. List<BackgroundItem> backgroundModFileTemplateList = new List<BackgroundItem>();
  55. for (int temp = 0; temp < Settings.Default.BackgroundTemplateList.Count; temp++)
  56. {
  57. if (Settings.Default.BackgroundTemplateList[temp].type == ComPDFKit.PDFDocument.C_Background_Type.BG_TYPE_IMAGE)
  58. {
  59. backgroundModFileTemplateList.Add(Settings.Default.BackgroundTemplateList[temp]);
  60. }
  61. }
  62. BackgroundModFileCollection = new ObservableCollection<BackgroundItem>(backgroundModFileTemplateList);
  63. CheckTemplateListIsEmpty(backgroundModFileTemplateList);
  64. }
  65. private void InitBackgroundTemplateList()
  66. {
  67. if (Settings.Default.BackgroundTemplateList == null)
  68. {
  69. Settings.Default.BackgroundTemplateList = new BackgroundTemplateList();
  70. }
  71. GetBackgroundSource();
  72. }
  73. private void UpdateBackgroundSources()
  74. {
  75. int count = backgroundModFileCollection.Count + backgroundModFileCollection.Count;
  76. List<BackgroundItem> backgroundModFileTemplateList = new List<BackgroundItem>();
  77. for (int temp = 0; temp < Settings.Default.BackgroundTemplateList.Count; temp++)
  78. {
  79. if (Settings.Default.BackgroundTemplateList[temp].type == ComPDFKit.PDFDocument.C_Background_Type.BG_TYPE_IMAGE)
  80. {
  81. backgroundModFileTemplateList.Add(Settings.Default.BackgroundTemplateList[temp]);
  82. }
  83. }
  84. BackgroundModFileCollection = new ObservableCollection<BackgroundItem>(backgroundModFileTemplateList);
  85. CheckTemplateListIsEmpty(backgroundModFileTemplateList);
  86. }
  87. public void AddTemplate()
  88. {
  89. var backgroundItem = new BackgroundItem();
  90. backgroundItem.pageRange = "0";
  91. backgroundItem.type = ComPDFKit.PDFDocument.C_Background_Type.BG_TYPE_IMAGE;
  92. backgroundItem.templateName += Settings.Default.BackgroundIndex.ToString();
  93. Settings.Default.BackgroundTemplateList.Add(backgroundItem);
  94. Settings.Default.Save();
  95. UpdateBackgroundSources();
  96. }
  97. public void DeleteTemplateItem(object e)
  98. {
  99. var btn = e as System.Windows.Controls.Button;
  100. if (btn == null)
  101. {
  102. return;
  103. }
  104. var template = btn.DataContext as BackgroundItem;
  105. if (template == null)
  106. {
  107. return;
  108. }
  109. Settings.Default.AppProperties.NeedToDeletePath.Add(template.previeImagePath);
  110. Settings.Default.AppProperties.NeedToDeletePath.Add(template.imagepath);
  111. Settings.Default.BackgroundTemplateList.Remove(template);
  112. Settings.Default.Save();
  113. BackgroundModFileCollection.Remove(template);
  114. UpdateBackgroundSources();
  115. }
  116. public bool IsNavigationTarget(NavigationContext navigationContext)
  117. {
  118. return true;
  119. }
  120. public void OnNavigatedFrom(NavigationContext navigationContext)
  121. {
  122. }
  123. public void OnNavigatedTo(NavigationContext navigationContext)
  124. {
  125. }
  126. }
  127. }