RadioButtonPropertyViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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(IDialogService dialogService)
  38. {
  39. dialogs = dialogService;
  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, 0xFF, 0x00, 0x00), Colors.Transparent);
  51. ResetColorThree = InitResetColor(Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Color.FromArgb(0xFF, 0x18, 0xA0, 0xFB), Colors.Transparent);
  52. ResetColorForth = InitResetColor(Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Color.FromArgb(0xFF, 0x10, 0xCB, 0x39), Colors.Transparent);
  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. RadioMemberNameTextChangedCommand = new DelegateCommand<string>(RadioMemberNameTextChanged);
  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 RadioMemberNameTextChanged(string obj)
  98. {
  99. if (obj != null && IsCurrentWidget == true)
  100. {
  101. RadioMemberName = 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. radioButtonArgs = null;
  201. isCreateWidget = false;
  202. IsCurrentWidget = false;
  203. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  204. }
  205. private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  206. {
  207. if (e != null && e.Count > 0)
  208. {
  209. var widgeArgs = e[e.Count - 1].EditAnnotArgs as WidgetArgs;
  210. if (widgeArgs != null)
  211. {
  212. AnnotEditEvent editEvent = e[e.Count - 1];
  213. if (editEvent.EditAction == ActionType.Modify)
  214. {
  215. SetSizeNoUpdateValue(widgeArgs.Width, widgeArgs.Height);
  216. }
  217. }
  218. }
  219. }
  220. public void OnNavigatedTo(NavigationContext navigationContext)
  221. {
  222. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  223. navigationContext.Parameters.TryGetValue<UpdateAttributeHelper>(ParameterNames.AnnotEvent, out AttribEvent);
  224. navigationContext.Parameters.TryGetValue<WidgetRadioButtonArgs>("WidgetArgs", out radioButtonArgs);
  225. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  226. PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler;
  227. GetWidgeText();
  228. UpdataSelectResetColorBtn();
  229. }
  230. private void GetWidgeText()
  231. {
  232. if (radioButtonArgs == null)
  233. {
  234. PDFViewer.SetMouseMode(MouseModes.FormEditTool);
  235. radioButtonArgs = new WidgetRadioButtonArgs();
  236. radioButtonArgs.BgColor = Colors.Transparent;
  237. radioButtonArgs.FontColor = Colors.Black;
  238. radioButtonArgs.LineColor = Colors.Black;
  239. radioButtonArgs.LineWidth = 2;
  240. radioButtonArgs.Width = 18;
  241. radioButtonArgs.Height = 18;
  242. radioButtonArgs.RadioMemberName = "Option1";
  243. PDFViewer.SetToolParam(radioButtonArgs);
  244. isCreateWidget = true;
  245. }
  246. else
  247. {
  248. PDFViewer.SetToolParam(new AnnotHandlerEventArgs());
  249. isCreateWidget = false;
  250. }
  251. GetProperty();
  252. IsCurrentWidget = true;
  253. }
  254. private void GetProperty()
  255. {
  256. if (radioButtonArgs != null)
  257. {
  258. IsLocked = radioButtonArgs.Locked;
  259. FieldName = radioButtonArgs.FieldName;
  260. ToolTipStr = radioButtonArgs.Tooltip;
  261. IsReadOnly = radioButtonArgs.ReadOnly;
  262. IsRequiredField = radioButtonArgs.IsRequired;
  263. FillColor = radioButtonArgs.BgColor;
  264. ContentColor = radioButtonArgs.FontColor;
  265. BorderColor = radioButtonArgs.LineColor;
  266. BorderThiness = radioButtonArgs.LineWidth;
  267. BorderStyle = radioButtonArgs.BorderStyle;
  268. RadioMemberName = radioButtonArgs.RadioMemberName;
  269. //避免BorderStyle跟上一个值相同,而没触发更改IsSolid属性
  270. if (BorderStyle == C_BORDER_STYLE.BS_SOLID)
  271. IsSolid = true;
  272. else
  273. IsSolid = false;
  274. IsDefaultRadioBox = radioButtonArgs.IsChecked;
  275. if (isCreateWidget == false)
  276. {
  277. HeightSize = radioButtonArgs.Height;
  278. WidthSize = radioButtonArgs.Width;
  279. }
  280. }
  281. }
  282. #endregion
  283. }
  284. }