CheckBoxPropertyViewModel.cs 11 KB

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