DecimalNumBox.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Mime;
  5. using System.Text;
  6. //----------------------------------------------------------------
  7. //Module Name: $safeprojectname$
  8. //Purpose:
  9. //CopyRight: Muchinfo
  10. //History:
  11. //----------------------------------------------------------------
  12. //DateTime 2016/8/24 11:03:31
  13. //Author
  14. //Description Create
  15. //----------------------------------------------------------------
  16. using System.Windows;
  17. using System.Windows.Input;
  18. using Xceed.Wpf.Toolkit;
  19. namespace Muchinfo.WPF.Controls
  20. {
  21. public class DecimalNumBox : DecimalUpDown
  22. {
  23. private string _tempText;
  24. /// <summary>
  25. /// 小数位数
  26. /// </summary>
  27. public int DecimalCount
  28. {
  29. get { return (int)GetValue(DecimalCountProperty); }
  30. set { SetValue(DecimalCountProperty, value); }
  31. }
  32. // Using a DependencyProperty as the backing store for DecimalCount. This enables animation, styling, binding, etc...
  33. public static readonly DependencyProperty DecimalCountProperty =
  34. DependencyProperty.Register("DecimalCount", typeof(int), typeof(DecimalNumBox), new PropertyMetadata(0));
  35. public int MaxTextLength
  36. {
  37. get { return (int)GetValue(MaxTextLengthProperty); }
  38. set { SetValue(MaxTextLengthProperty, value); }
  39. }
  40. // Using a DependencyProperty as the backing store for MaxTextLength. This enables animation, styling, binding, etc...
  41. public static readonly DependencyProperty MaxTextLengthProperty =
  42. DependencyProperty.Register("MaxTextLength", typeof(int), typeof(DecimalNumBox), new PropertyMetadata(20));
  43. protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e)
  44. {
  45. _tempText = Text;
  46. if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || (e.Key >= Key.D0 && e.Key <= Key.D9))
  47. {
  48. if (!string.IsNullOrWhiteSpace(Text) && Text.Contains('.') && DecimalCount>0)
  49. {
  50. string[] strArr = TextBox.Text.Split('.');
  51. if (strArr.Length >0&&TextBox.CaretIndex <= strArr[0].Length)
  52. {
  53. e.Handled = false;
  54. }
  55. else if (strArr.Length >= 1 && TextBox.CaretIndex > strArr[0].Length&&strArr[1].Length < DecimalCount)
  56. {
  57. e.Handled = false;
  58. }
  59. else
  60. {
  61. e.Handled = true;
  62. }
  63. }
  64. else
  65. {
  66. e.Handled = false;
  67. }
  68. }
  69. else if (((e.Key == Key.OemPeriod) || e.Key == Key.Decimal) && DecimalCount>0&& !string.IsNullOrWhiteSpace(Text) && !Text.Contains('.'))
  70. {
  71. e.Handled = false;
  72. }
  73. else if (e.Key == Key.Tab)
  74. {
  75. e.Handled = false;
  76. }
  77. else
  78. {
  79. e.Handled = true;
  80. }
  81. if (!string.IsNullOrWhiteSpace(Text)&&MaxTextLength <= Text.Length)
  82. {
  83. e.Handled = true;
  84. }
  85. }
  86. protected override void OnKeyUp(KeyEventArgs e)
  87. {
  88. var index = TextBox.CaretIndex;
  89. //base.OnKeyUp(e);
  90. if (string.IsNullOrWhiteSpace(Text))
  91. {
  92. this.Value = 0;
  93. return;
  94. }
  95. if (DecimalCount > 0)
  96. {
  97. var pattern = "^[0-9]+(.[0-9]{0," + DecimalCount + "})?$";
  98. if (!System.Text.RegularExpressions.Regex.IsMatch(Text, pattern))
  99. {
  100. Text = _tempText;
  101. this.Value= this.ConvertTextToValue(Text);
  102. TextBox.CaretIndex = index;
  103. }
  104. }
  105. }
  106. }
  107. }