CheckBoxPropertyViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFAnnotation.Form;
  3. using ComPDFKitViewer;
  4. using ComPDFKitViewer.AnnotEvent;
  5. using ComPDFKitViewer.PdfViewer;
  6. using PDF_Master.Helper;
  7. using PDF_Master.Model;
  8. using Prism.Commands;
  9. using Prism.Mvvm;
  10. using Prism.Regions;
  11. using Prism.Services.Dialogs;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows.Media;
  18. namespace PDF_Master.ViewModels.Form
  19. {
  20. public class CheckBoxPropertyViewModel : FormBaseVM, INavigationAware
  21. {
  22. #region Command
  23. public DelegateCommand<string> FieldNameTextChangedCommand { get; set; }
  24. public DelegateCommand<string> ToolTipTextChangedCommand { get; set; }
  25. //外观
  26. public DelegateCommand<object> ResetColorCommand { get; set; }
  27. public DelegateCommand<object> ResetColorCheckedBtnCommand { get; set; }
  28. public DelegateCommand<string> ExportedValuesTextChangedCommand { get; set; }
  29. public DelegateCommand<object> LineStyleCommand { get; set; }
  30. #endregion
  31. #region 变量
  32. private CPDFViewer PDFViewer;
  33. private WidgetCheckBoxArgs checkBoxArgs;
  34. private IDialogService dialogs;
  35. public event EventHandler<int> SelectResetColorBtnHandler;
  36. #endregion
  37. #region 初始化
  38. public CheckBoxPropertyViewModel(IDialogService dialogService)
  39. {
  40. dialogs = dialogService;
  41. InitVariable();
  42. InitCommand();
  43. }
  44. private void InitVariable()
  45. {
  46. InitAllResetColor();
  47. }
  48. private void InitAllResetColor()
  49. {
  50. ResetColorOne = InitResetColor(Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Colors.Transparent);
  51. ResetColorTwo = InitResetColor(Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF));
  52. ResetColorThree = InitResetColor(Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Color.FromArgb(0xFF, 0xFF, 0x00, 0x00), Colors.Transparent);
  53. ResetColorForth = InitResetColor(Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Color.FromArgb(0xFF, 0xff, 0x00, 0x00), Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF));
  54. }
  55. private void InitCommand()
  56. {
  57. //一般
  58. FieldNameTextChangedCommand = new DelegateCommand<string>(FieldNameTextChanged);
  59. ToolTipTextChangedCommand = new DelegateCommand<string>(ToolTipTextChanged);
  60. //外观
  61. ResetColorCheckedBtnCommand = new DelegateCommand<object>(ResetColorCheckedBtn);
  62. ResetColorCommand = new DelegateCommand<object>(ResetColorEvent);
  63. LineStyleCommand = new DelegateCommand<object>(LineStyleBtnEvent);
  64. //选项
  65. ExportedValuesTextChangedCommand = new DelegateCommand<string>(ExportedValuesTextChanged);
  66. }
  67. #endregion
  68. #region 事件
  69. private void FieldNameTextChanged(string obj)
  70. {
  71. if (string.IsNullOrEmpty(obj) == false && IsCurrentWidget == true)
  72. {
  73. FieldName = obj;
  74. }
  75. }
  76. private void ToolTipTextChanged(string obj)
  77. {
  78. if (string.IsNullOrEmpty(obj) == false && IsCurrentWidget == true)
  79. {
  80. ToolTipStr = obj;
  81. }
  82. }
  83. private void LineStyleBtnEvent(object obj)
  84. {
  85. if (obj != null)
  86. {
  87. switch ((string)obj)
  88. {
  89. case "Solid":
  90. BorderStyle = ComPDFKit.PDFAnnotation.C_BORDER_STYLE.BS_SOLID;
  91. break;
  92. case "Dotted":
  93. BorderStyle = ComPDFKit.PDFAnnotation.C_BORDER_STYLE.BS_DASHDED;
  94. break;
  95. }
  96. }
  97. }
  98. private void ExportedValuesTextChanged(string obj)
  99. {
  100. if (obj != null && IsCurrentWidget == true)
  101. {
  102. ExportedValues = obj;
  103. }
  104. }
  105. private void ResetColorCheckedBtn(object obj)
  106. {
  107. if (obj != null)
  108. {
  109. var str = obj as string;
  110. if (str != null)
  111. {
  112. switch (str)
  113. {
  114. case "One":
  115. BorderColor = ResetColorOne.BorderColor.Color;
  116. ContentColor = ResetColorOne.FontColor.Color;
  117. FillColor = ResetColorOne.FillColor.Color;
  118. break;
  119. case "Two":
  120. BorderColor = ResetColorTwo.BorderColor.Color;
  121. ContentColor = ResetColorTwo.FontColor.Color;
  122. FillColor = ResetColorTwo.FillColor.Color;
  123. break;
  124. case "Three":
  125. BorderColor = ResetColorThree.BorderColor.Color;
  126. ContentColor = ResetColorThree.FontColor.Color;
  127. FillColor = ResetColorThree.FillColor.Color;
  128. break;
  129. case "Forth":
  130. BorderColor = ResetColorForth.BorderColor.Color;
  131. ContentColor = ResetColorForth.FontColor.Color;
  132. FillColor = ResetColorForth.FillColor.Color;
  133. break;
  134. }
  135. }
  136. }
  137. }
  138. private void ResetColorEvent(object obj)
  139. {
  140. bool result = true;
  141. DialogParameters value = new DialogParameters();
  142. value.Add(ParameterNames.PDFViewer, PDFViewer);
  143. dialogs.ShowDialog(DialogNames.EditPresetColorsDialog, value, e =>
  144. {
  145. if (e.Result != ButtonResult.OK)
  146. {
  147. result = false;
  148. }
  149. EditPresetColorsDialogViewModel DialogVM = e.Parameters.GetValue<EditPresetColorsDialogViewModel>(ParameterNames.DataModel);
  150. if (DialogVM != null)
  151. {
  152. }
  153. });
  154. if (!result)
  155. {
  156. return;
  157. }
  158. }
  159. #endregion
  160. #region 外部XAML触发事件
  161. private void UpdataSelectResetColorBtn()
  162. {
  163. int result = 0;
  164. if (UpdataSelectResetColor(ResetColorOne))
  165. {
  166. result = 1;
  167. }
  168. else if (UpdataSelectResetColor(ResetColorTwo))
  169. {
  170. result = 2;
  171. }
  172. else if (UpdataSelectResetColor(ResetColorThree))
  173. {
  174. result = 3;
  175. }
  176. else if (UpdataSelectResetColor(ResetColorForth))
  177. {
  178. result = 4;
  179. }
  180. SelectResetColorBtnHandler?.Invoke(null, result);
  181. }
  182. private bool UpdataSelectResetColor(ResetColor reset)
  183. {
  184. if (reset.FillColor.Color == FillColor &&
  185. reset.FontColor.Color == ContentColor &&
  186. reset.BorderColor.Color == BorderColor
  187. )
  188. {
  189. return true;
  190. }
  191. return false;
  192. }
  193. #endregion
  194. #region Navegation
  195. public bool IsNavigationTarget(NavigationContext navigationContext)
  196. {
  197. return true;
  198. }
  199. public void OnNavigatedFrom(NavigationContext navigationContext)
  200. {
  201. checkBoxArgs = null;
  202. isCreateWidget = false;
  203. IsCurrentWidget = false;
  204. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  205. }
  206. public void OnNavigatedTo(NavigationContext navigationContext)
  207. {
  208. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  209. navigationContext.Parameters.TryGetValue<UpdateAttributeHelper>(ParameterNames.AnnotEvent, out AttribEvent);
  210. navigationContext.Parameters.TryGetValue<WidgetCheckBoxArgs>("WidgetArgs", out checkBoxArgs);
  211. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  212. PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler;
  213. GetWidgeText();
  214. UpdataSelectResetColorBtn();
  215. }
  216. private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  217. {
  218. if (e != null && e.Count > 0)
  219. {
  220. var widgeArgs = e[e.Count - 1].EditAnnotArgs as WidgetArgs;
  221. if (widgeArgs != null)
  222. {
  223. AnnotEditEvent editEvent = e[e.Count - 1];
  224. if (editEvent.EditAction == ActionType.Modify)
  225. {
  226. SetSizeNoUpdateValue(widgeArgs.Width, widgeArgs.Height);
  227. }
  228. }
  229. }
  230. }
  231. private void GetWidgeText()
  232. {
  233. if (checkBoxArgs == null)
  234. {
  235. PDFViewer.SetMouseMode(MouseModes.FormEditTool);
  236. checkBoxArgs = new WidgetCheckBoxArgs();
  237. WidgetCheckBoxArgs args = new WidgetCheckBoxArgs();
  238. args.CheckStyle = C_CHECK_STYLE.CK_CHECK;
  239. args.BorderStyle = C_BORDER_STYLE.BS_SOLID;
  240. args.LineColor = Colors.Black;
  241. args.BgColor = Colors.LightGreen;
  242. args.LineWidth = 2;
  243. args.Height = 18;
  244. args.Width = 18;
  245. args.ExportValue = "Yes";
  246. checkBoxArgs = args;
  247. PDFViewer.SetToolParam(checkBoxArgs);
  248. isCreateWidget = true;
  249. }
  250. else
  251. {
  252. PDFViewer.SetToolParam(new AnnotHandlerEventArgs());
  253. isCreateWidget = false;
  254. }
  255. GetProperty();
  256. IsCurrentWidget = true;
  257. }
  258. private void GetProperty()
  259. {
  260. if (checkBoxArgs != null)
  261. {
  262. IsLocked = checkBoxArgs.Locked;
  263. FieldName = checkBoxArgs.FieldName;
  264. ToolTipStr = checkBoxArgs.Tooltip;
  265. IsReadOnly = checkBoxArgs.ReadOnly;
  266. IsRequiredField = checkBoxArgs.IsRequired;
  267. FillColor = checkBoxArgs.BgColor;
  268. ContentColor = checkBoxArgs.FontColor;
  269. BorderColor = checkBoxArgs.LineColor;
  270. BorderThiness = checkBoxArgs.LineWidth;
  271. BorderStyle = checkBoxArgs.BorderStyle;
  272. ExportedValues = checkBoxArgs.ExportValue;
  273. //避免BorderStyle跟上一个值相同,而没触发更改IsSolid属性
  274. if (BorderStyle == C_BORDER_STYLE.BS_SOLID)
  275. IsSolid = true;
  276. else
  277. IsSolid = false;
  278. IsDefaultCheckBox = checkBoxArgs.IsChecked;
  279. if (isCreateWidget == false)
  280. {
  281. HeightSize = checkBoxArgs.Height;
  282. WidthSize = checkBoxArgs.Width;
  283. }
  284. }
  285. }
  286. #endregion
  287. }
  288. }