RadioButtonPropertyViewModel.cs 11 KB

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