TextInputControl.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System.Linq;
  2. using System.Windows.Controls;
  3. using FWindSoft.Tools;
  4. namespace FWindSoft.Wpf.Controls
  5. {
  6. //输入控制
  7. public interface ITextInputControl
  8. {
  9. bool InputControl(string fullText);
  10. ItemCollection Parmerters { get; }
  11. }
  12. //为使用调用ItemCollection封装基类,如果在子类定义临时属性也可以
  13. public abstract class BaseTextInputControl : ITextInputControl
  14. {
  15. private ItemCollection m_Paramters;
  16. protected BaseTextInputControl()
  17. {
  18. ComboBox com = new ComboBox();
  19. this.m_Paramters = com.Items;
  20. this.m_Paramters.Clear();
  21. }
  22. public abstract bool InputControl(string fullText);
  23. public ItemCollection Parmerters
  24. {
  25. get { return this.m_Paramters; }
  26. }
  27. }
  28. #region 控件输入验证族
  29. public class AccessInteger : BaseTextInputControl
  30. {
  31. public override bool InputControl(string fullText)
  32. {
  33. if (fullText == null)
  34. return false;
  35. if (fullText.Length == 1)
  36. {
  37. if ("-".Equals(fullText) || RegexUtil.IsInteger(fullText))
  38. {
  39. return true;
  40. }
  41. else
  42. {
  43. return false;
  44. }
  45. }
  46. return RegexUtil.IsInteger(fullText);
  47. }
  48. }
  49. public class AccessPlusInteger : BaseTextInputControl
  50. {
  51. public override bool InputControl(string fullText)
  52. {
  53. return RegexUtil.IsPlusInteger(fullText);
  54. }
  55. }
  56. public class AccessDecimal : BaseTextInputControl
  57. {
  58. public override bool InputControl(string fullText)
  59. {
  60. if (fullText.Length == 1)
  61. {
  62. if ("-".Equals(fullText) || RegexUtil.IsDecimal(fullText))
  63. {
  64. return true;
  65. }
  66. else
  67. {
  68. return false;
  69. }
  70. }
  71. return RegexUtil.IsDecimal(fullText) || (fullText.Count(c => c == '.') == 1 && fullText.IndexOf('.') == fullText.Length - 1 && fullText.IndexOf("-.") < 0);//有一个小数点且在末尾
  72. }
  73. }
  74. public class AccessPlusDecimal : BaseTextInputControl
  75. {
  76. public override bool InputControl(string fullText)
  77. {
  78. if (fullText.Length == 1)
  79. {
  80. if (".".Equals(fullText))
  81. return false;
  82. }
  83. return RegexUtil.IsPlusDecimal(fullText) || (fullText.Count(c => c == '.') == 1 && fullText.IndexOf('.') == fullText.Length - 1);//有一个小数点且在末尾
  84. }
  85. }
  86. public class AccessRangeDecimal : BaseTextInputControl
  87. {
  88. public double MaxValue { set; get; }
  89. public double MinValue { set; get; }
  90. public override bool InputControl(string fullText)
  91. {
  92. double value;
  93. if (double.TryParse(fullText, out value))
  94. {
  95. if (value >= MinValue && value <= MaxValue)
  96. return true;
  97. else
  98. return false;
  99. }
  100. else
  101. {
  102. return false;
  103. }
  104. }
  105. }
  106. #endregion
  107. }