TextFieldPropertyViewModel.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using ComPDFKitViewer.PdfViewer;
  4. using PDF_Office.Helper;
  5. using PDF_Office.Model;
  6. using PDF_Office.Model.From;
  7. using Prism.Commands;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Media;
  16. namespace PDF_Office.ViewModels.Form
  17. {
  18. public class TextFieldPropertyViewModel : FormBaseVM, INavigationAware
  19. {
  20. #region 属性
  21. #region 一般
  22. private FormFieldType _formPos;
  23. public FormFieldType FormPos
  24. {
  25. get { return _formPos; }
  26. set { SetProperty(ref _formPos, value); ChangeFieldValue("FormPos"); }
  27. }
  28. #endregion
  29. #region 选项
  30. //默认值
  31. private string _defaultValue;
  32. public string DefaultValue
  33. {
  34. get { return _defaultValue; }
  35. set { SetProperty(ref _defaultValue, value); }
  36. }
  37. //多行
  38. private bool _isMultiline = false;
  39. public bool IsMultiLine
  40. {
  41. get { return _isMultiline; }
  42. set { SetProperty(ref _isMultiline, value); }
  43. }
  44. //滚动显示长文本
  45. private bool _isScrollText = false;
  46. public bool IsScrollText
  47. {
  48. get { return _isScrollText; }
  49. set { SetProperty(ref _isScrollText, value); }
  50. }
  51. #endregion
  52. #endregion
  53. #region Command变量
  54. public DelegateCommand<string> NameTextChangedCommand { get; set; }
  55. public DelegateCommand<string> ToolTipTextChangedCommand { get; set; }
  56. public DelegateCommand<object> IsReadOnlyCheckedCommand { get; set; }
  57. public DelegateCommand<object> RequiredFieldCheckedCommand { get; set; }
  58. #endregion
  59. #region 变量
  60. private CPDFViewer PDFViewer;
  61. private WidgetTextBoxArgs textBoxArgs;
  62. public UpdateAttributeHelper AttribEvent;
  63. #endregion
  64. #region 初始化
  65. public TextFieldPropertyViewModel()
  66. {
  67. InitVariable();
  68. InitCommand();
  69. }
  70. private void InitVariable()
  71. {
  72. }
  73. private void InitCommand()
  74. {
  75. NameTextChangedCommand = new DelegateCommand<string>(NameTextChanged);
  76. ToolTipTextChangedCommand = new DelegateCommand<string>(ToolTipTextChanged);
  77. IsReadOnlyCheckedCommand = new DelegateCommand<object>(IsReadOnlyChecked);
  78. RequiredFieldCheckedCommand = new DelegateCommand<object>(RequiredFieldChecked);
  79. ChangeValueHandler -= ChangeValue;
  80. ChangeValueHandler += ChangeValue;
  81. }
  82. #endregion
  83. #region 一般处理
  84. private void NameTextChanged(string obj)
  85. {
  86. if(string.IsNullOrEmpty(obj) == false)
  87. {
  88. NameStr = obj;
  89. }
  90. }
  91. private void ToolTipTextChanged(string obj)
  92. {
  93. if (string.IsNullOrEmpty(obj) == false)
  94. {
  95. ToolTipStr = obj;
  96. }
  97. }
  98. private void IsReadOnlyChecked(object obj)
  99. {
  100. if(obj is bool)
  101. {
  102. IsReadOnly = (bool)obj;
  103. }
  104. }
  105. private void RequiredFieldChecked(object obj)
  106. {
  107. if (obj is bool)
  108. {
  109. IsRequiredField = (bool)obj;
  110. }
  111. }
  112. #endregion
  113. #region 外观处理
  114. #endregion
  115. #region 选项处理
  116. #endregion
  117. #region Navegation
  118. public bool IsNavigationTarget(NavigationContext navigationContext)
  119. {
  120. return true;
  121. }
  122. public void OnNavigatedFrom(NavigationContext navigationContext)
  123. {
  124. textBoxArgs = null;
  125. }
  126. public void OnNavigatedTo(NavigationContext navigationContext)
  127. {
  128. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  129. navigationContext.Parameters.TryGetValue<UpdateAttributeHelper>(ParameterNames.AnnotEvent, out AttribEvent);
  130. navigationContext.Parameters.TryGetValue<WidgetTextBoxArgs>("WidgetArgs", out textBoxArgs);
  131. GetWidgeText();
  132. }
  133. private void GetWidgeText()
  134. {
  135. PDFViewer.SetMouseMode(MouseModes.FormEditTool);
  136. if (textBoxArgs == null)
  137. {
  138. WidgetTextBoxArgs textArgs = new WidgetTextBoxArgs();
  139. textArgs.BgColor = Colors.Transparent;
  140. textArgs.FontSize = 12;
  141. textArgs.FontFamily = "Courier New";
  142. textArgs.FontColor = Colors.Black;
  143. textArgs.FieldName = "TextBox";
  144. textBoxArgs = textArgs;
  145. PDFViewer.SetToolParam(textBoxArgs);
  146. }
  147. else
  148. {
  149. PDFViewer.SetToolParam(new AnnotHandlerEventArgs());
  150. }
  151. GetProperty();
  152. }
  153. private void GetProperty()
  154. {
  155. if(textBoxArgs != null)
  156. {
  157. NameStr = textBoxArgs.FieldName;
  158. IsRequiredField = textBoxArgs.IsRequired;
  159. IsMultiLine = textBoxArgs.IsMultiLine;
  160. }
  161. }
  162. //更改基类公共属性后,触发的事件
  163. private void ChangeValue(object sender, FormAttributeType e)
  164. {
  165. switch(e)
  166. {
  167. case FormAttributeType.Name:
  168. textBoxArgs.FieldName = NameStr;
  169. AttribEvent?.UpdateAttrib(AnnotAttrib.FieldName, NameStr);
  170. break;
  171. case FormAttributeType.ToolTip:
  172. textBoxArgs.Tooltip = ToolTipStr;
  173. AttribEvent?.UpdateAttrib(AnnotAttrib.Tooltip, ToolTipStr);
  174. break;
  175. case FormAttributeType.IsSolid:
  176. textBoxArgs.BorderStyle = IsSolid;
  177. AttribEvent?.UpdateAttrib(AnnotAttrib.LineStyle, IsSolid);
  178. break;
  179. case FormAttributeType.IsLocked:
  180. textBoxArgs.Locked = IsLocked;
  181. AttribEvent?.UpdateAttrib(AnnotAttrib.Locked, IsLocked);
  182. break;
  183. case FormAttributeType.HeightSize:
  184. textBoxArgs.DefaultWidth = HeightSize;
  185. //AttribEvent?.UpdateAttrib(, BorderThiness);
  186. break;
  187. case FormAttributeType.BorderThiness:
  188. textBoxArgs.LineWidth = BorderThiness;
  189. AttribEvent?.UpdateAttrib(AnnotAttrib.Thickness, BorderThiness);
  190. break;
  191. case FormAttributeType.BorderColor:
  192. textBoxArgs.LineColor = BorderColor;
  193. AttribEvent?.UpdateAttrib(AnnotAttrib.Color, BorderColor);
  194. break;
  195. case FormAttributeType.ContentColor:
  196. textBoxArgs.FontColor = ContentColor;
  197. AttribEvent?.UpdateAttrib(AnnotAttrib.FontColor, ContentColor);
  198. break;
  199. case FormAttributeType.IsReadOnly:
  200. textBoxArgs.ReadOnly = IsReadOnly;
  201. AttribEvent?.UpdateAttrib(AnnotAttrib.ReadOnly, IsReadOnly);
  202. break;
  203. case FormAttributeType.WidthSize:
  204. break;
  205. case FormAttributeType.IsRequiredField:
  206. textBoxArgs.IsRequired = IsRequiredField;
  207. AttribEvent?.UpdateAttrib(AnnotAttrib.IsRequired, IsRequiredField);
  208. break;
  209. }
  210. AttribEvent?.UpdateAnnot();
  211. }
  212. private void ChangeFieldValue(string tag)
  213. {
  214. switch(tag)
  215. {
  216. case "FormPos":
  217. AttribEvent?.UpdateAttrib(AnnotAttrib.FormField,FormPos);
  218. break;
  219. }
  220. AttribEvent?.UpdateAnnot();
  221. }
  222. #endregion
  223. }
  224. }