ListBoxPropertyViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using ComPDFKitViewer.PdfViewer;
  4. using PDF_Office.CustomControl.CompositeControl;
  5. using PDF_Office.Helper;
  6. using PDF_Office.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;
  17. using System.Windows.Media;
  18. namespace PDF_Office.ViewModels.Form
  19. {
  20. public class ListBoxPropertyViewModel : 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> FormContentTextChangedCommand { get; set; }
  29. public DelegateCommand<object> LineStyleCommand { get; set; }
  30. #endregion
  31. #region 变量
  32. private CPDFViewer PDFViewer;
  33. private WidgetListBoxArgs listBoxArgs;
  34. private IDialogService dialogs;
  35. public event EventHandler<int> SelectResetColorBtnHandler;
  36. public List<ComboDataItem> FontFamilyItems { get; private set; }
  37. public List<ComboDataItem> FontStyleItems { get; private set; }
  38. public List<ComboDataItem> AglinmentItems { get; private set; }
  39. #endregion
  40. #region 初始化
  41. public ListBoxPropertyViewModel()
  42. {
  43. InitVariable();
  44. InitCommand();
  45. }
  46. private void InitVariable()
  47. {
  48. InitAllResetColor();
  49. InitFontFamilyComboBox();
  50. InitFontStyleComboBox();
  51. InitAglinmentItemsComboBox();
  52. }
  53. private void InitAllResetColor()
  54. {
  55. ResetColorOne = InitResetColor(Colors.Transparent, Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Colors.Transparent);
  56. ResetColorTwo = InitResetColor(Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Colors.Transparent);
  57. ResetColorThree = InitResetColor(Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Colors.Transparent);
  58. ResetColorForth = InitResetColor(Color.FromArgb(0xFF, 0xff, 0x00, 0x00), Color.FromArgb(0xFF, 0xff, 0x00, 0x00), Colors.Transparent);
  59. }
  60. private void InitFontFamilyComboBox()
  61. {
  62. FontFamilyItems = new List<ComboDataItem>();
  63. ComboDataItem item = new ComboDataItem("Courier", "Courier New");
  64. FontFamilyItems.Add(item);
  65. item = new ComboDataItem("Helvetica", "Helvetica");
  66. FontFamilyItems.Add(item);
  67. item = new ComboDataItem("Times Roman", "Times New Roman");
  68. FontFamilyItems.Add(item);
  69. }
  70. private void InitFontStyleComboBox()
  71. {
  72. FontStyleItems = new List<ComboDataItem>();
  73. ComboDataItem item = new ComboDataItem("Regular", "Regular");
  74. FontStyleItems.Add(item);
  75. item = new ComboDataItem("Bold", "Bold");
  76. FontStyleItems.Add(item);
  77. item = new ComboDataItem("Italic", "Italic");
  78. FontStyleItems.Add(item);
  79. item = new ComboDataItem("Bold Italic", "Bold Italic");
  80. FontStyleItems.Add(item);
  81. }
  82. private void InitAglinmentItemsComboBox()
  83. {
  84. AglinmentItems = new List<ComboDataItem>();
  85. ComboDataItem item = new ComboDataItem("Left", "Left");
  86. AglinmentItems.Add(item);
  87. item = new ComboDataItem("Center", "Center");
  88. AglinmentItems.Add(item);
  89. item = new ComboDataItem("Right", "Right");
  90. AglinmentItems.Add(item);
  91. }
  92. private void InitCommand()
  93. {
  94. //一般
  95. FieldNameTextChangedCommand = new DelegateCommand<string>(FieldNameTextChanged);
  96. ToolTipTextChangedCommand = new DelegateCommand<string>(ToolTipTextChanged);
  97. //外观
  98. ResetColorCheckedBtnCommand = new DelegateCommand<object>(ResetColorCheckedBtn);
  99. ResetColorCommand = new DelegateCommand<object>(ResetColorEvent);
  100. LineStyleCommand = new DelegateCommand<object>(LineStyleBtnEvent);
  101. //选项
  102. FormContentTextChangedCommand = new DelegateCommand<string>(FormContentTextChanged);
  103. }
  104. #endregion
  105. #region 事件
  106. private void FieldNameTextChanged(string obj)
  107. {
  108. if (string.IsNullOrEmpty(obj) == false && IsCurrentWidget == true)
  109. {
  110. FieldName = obj;
  111. }
  112. }
  113. private void ToolTipTextChanged(string obj)
  114. {
  115. if (string.IsNullOrEmpty(obj) == false && IsCurrentWidget == true)
  116. {
  117. ToolTipStr = obj;
  118. }
  119. }
  120. private void LineStyleBtnEvent(object obj)
  121. {
  122. if (obj != null)
  123. {
  124. switch ((string)obj)
  125. {
  126. case "Solid":
  127. BorderStyle = ComPDFKit.PDFAnnotation.C_BORDER_STYLE.BS_SOLID;
  128. break;
  129. case "Dotted":
  130. BorderStyle = ComPDFKit.PDFAnnotation.C_BORDER_STYLE.BS_DASHDED;
  131. break;
  132. }
  133. }
  134. }
  135. private void FormContentTextChanged(string obj)
  136. {
  137. if (obj != null && IsCurrentWidget == true)
  138. {
  139. FormContent = obj;
  140. }
  141. }
  142. private void ResetColorCheckedBtn(object obj)
  143. {
  144. if (obj != null)
  145. {
  146. var str = obj as string;
  147. if (str != null)
  148. {
  149. switch (str)
  150. {
  151. case "One":
  152. BorderColor = ResetColorOne.BorderColor.Color;
  153. ContentColor = ResetColorOne.FontColor.Color;
  154. FillColor = ResetColorOne.FillColor.Color;
  155. break;
  156. case "Two":
  157. BorderColor = ResetColorTwo.BorderColor.Color;
  158. ContentColor = ResetColorTwo.FontColor.Color;
  159. FillColor = ResetColorTwo.FillColor.Color;
  160. break;
  161. case "Three":
  162. BorderColor = ResetColorThree.BorderColor.Color;
  163. ContentColor = ResetColorThree.FontColor.Color;
  164. FillColor = ResetColorThree.FillColor.Color;
  165. break;
  166. case "Forth":
  167. BorderColor = ResetColorForth.BorderColor.Color;
  168. ContentColor = ResetColorForth.FontColor.Color;
  169. FillColor = ResetColorForth.FillColor.Color;
  170. break;
  171. }
  172. }
  173. }
  174. }
  175. private void ResetColorEvent(object obj)
  176. {
  177. bool result = true;
  178. DialogParameters value = new DialogParameters();
  179. value.Add(ParameterNames.PDFViewer, PDFViewer);
  180. dialogs.ShowDialog(DialogNames.EditPresetColorsDialog, value, e =>
  181. {
  182. if (e.Result != ButtonResult.OK)
  183. {
  184. result = false;
  185. }
  186. EditPresetColorsDialogViewModel DialogVM = e.Parameters.GetValue<EditPresetColorsDialogViewModel>(ParameterNames.DataModel);
  187. if (DialogVM != null)
  188. {
  189. }
  190. });
  191. if (!result)
  192. {
  193. return;
  194. }
  195. }
  196. #endregion
  197. #region 外部XAML触发事件
  198. private void UpdataSelectResetColorBtn()
  199. {
  200. int result = 0;
  201. if (UpdataSelectResetColor(ResetColorOne))
  202. {
  203. result = 1;
  204. }
  205. else if (UpdataSelectResetColor(ResetColorTwo))
  206. {
  207. result = 2;
  208. }
  209. else if (UpdataSelectResetColor(ResetColorThree))
  210. {
  211. result = 3;
  212. }
  213. else if (UpdataSelectResetColor(ResetColorForth))
  214. {
  215. result = 4;
  216. }
  217. SelectResetColorBtnHandler?.Invoke(null, result);
  218. }
  219. private bool UpdataSelectResetColor(ResetColor reset)
  220. {
  221. if (reset.FillColor.Color == FillColor &&
  222. reset.FontColor.Color == ContentColor &&
  223. reset.BorderColor.Color == BorderColor
  224. )
  225. {
  226. return true;
  227. }
  228. return false;
  229. }
  230. #endregion
  231. #region Navegation
  232. public bool IsNavigationTarget(NavigationContext navigationContext)
  233. {
  234. return true;
  235. }
  236. public void OnNavigatedFrom(NavigationContext navigationContext)
  237. {
  238. listBoxArgs = null;
  239. isCreateWidget = false;
  240. IsCurrentWidget = false;
  241. }
  242. public void OnNavigatedTo(NavigationContext navigationContext)
  243. {
  244. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  245. navigationContext.Parameters.TryGetValue<WidgetListBoxArgs>("WidgetArgs", out listBoxArgs);
  246. navigationContext.Parameters.TryGetValue<UpdateAttributeHelper>(ParameterNames.AnnotEvent, out AttribEvent);
  247. GetWidgeText();
  248. UpdataSelectResetColorBtn();
  249. }
  250. private void GetWidgeText()
  251. {
  252. if (listBoxArgs == null)
  253. {
  254. PDFViewer.SetMouseMode(MouseModes.FormEditTool);
  255. listBoxArgs = new WidgetListBoxArgs();
  256. listBoxArgs.BgColor = Colors.White;
  257. listBoxArgs.FontFamily = "Courier New";
  258. listBoxArgs.FontSize = 12;
  259. listBoxArgs.FontColor = Colors.Black;
  260. listBoxArgs.LineColor = Colors.Black;
  261. listBoxArgs.LineWidth = 1;
  262. PDFViewer.SetToolParam(listBoxArgs);
  263. isCreateWidget = true;
  264. }
  265. else
  266. {
  267. PDFViewer.SetToolParam(new AnnotHandlerEventArgs());
  268. isCreateWidget = false;
  269. }
  270. GetProperty();
  271. IsCurrentWidget = true;
  272. }
  273. private void GetProperty()
  274. {
  275. if (listBoxArgs != null)
  276. {
  277. IsLocked = listBoxArgs.Locked;
  278. FieldName = listBoxArgs.FieldName;
  279. ToolTipStr = listBoxArgs.Tooltip;
  280. IsReadOnly = listBoxArgs.ReadOnly;
  281. FillColor = listBoxArgs.BgColor;
  282. ContentColor = listBoxArgs.FontColor;
  283. BorderColor = listBoxArgs.LineColor;
  284. BorderThiness = listBoxArgs.LineWidth;
  285. BorderStyle = listBoxArgs.BorderStyle;
  286. string fontWeightStyleStr = "";
  287. if (listBoxArgs.FontStyle == FontStyles.Normal)
  288. {
  289. if (listBoxArgs.FontWeight == FontWeights.Normal)
  290. fontWeightStyleStr = "Regular";
  291. else
  292. fontWeightStyleStr = "Bold";
  293. }
  294. else
  295. {
  296. if (listBoxArgs.FontWeight == FontWeights.Normal)
  297. fontWeightStyleStr = "Italic";
  298. else
  299. fontWeightStyleStr = "Bold Italic";
  300. }
  301. FontWeightStyleItem = new ComboDataItem(fontWeightStyleStr);
  302. FontFamilyData = new ComboDataItem(listBoxArgs.FontFamily);
  303. //避免BorderStyle跟上一个值相同,而没触发更改IsSolid属性
  304. if (BorderStyle == C_BORDER_STYLE.BS_SOLID)
  305. IsSolid = true;
  306. else
  307. IsSolid = false;
  308. FontSizeData = new ComboDataItem(listBoxArgs.FontSize);
  309. if (isCreateWidget == false)
  310. {
  311. HeightSize = listBoxArgs.Height;
  312. WidthSize = listBoxArgs.Width;
  313. }
  314. //FormContent = listBoxArgs.Text;
  315. //IsMultiLine = listBoxArgs.IsMultiLine;
  316. //IsScrollText = listBoxArgs.ScrollFlag;
  317. }
  318. }
  319. #endregion
  320. }
  321. }