AITBarcodeTextBox.xaml.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace Aitex.Core.UI.DeviceControl
  16. {
  17. /// <summary>
  18. /// AITBarcodeTextBox.xaml 的交互逻辑
  19. /// </summary>
  20. public partial class AITBarcodeTextBox : UserControl
  21. {
  22. public static readonly DependencyProperty BarcodeTextProperty = DependencyProperty.Register(
  23. "BarcodeText", typeof(string), typeof(AITBarcodeTextBox),
  24. new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsRender));
  25. public string BarcodeText
  26. {
  27. get
  28. {
  29. return (string)this.GetValue(BarcodeTextProperty);
  30. }
  31. set
  32. {
  33. this.SetValue(BarcodeTextProperty, value);
  34. }
  35. }
  36. public static readonly DependencyProperty BarcodeLengthProperty = DependencyProperty.Register(
  37. "BarcodeLength", typeof(int), typeof(AITBarcodeTextBox),
  38. new FrameworkPropertyMetadata(6, FrameworkPropertyMetadataOptions.AffectsRender));
  39. public int BarcodeLength
  40. {
  41. get
  42. {
  43. return (int)this.GetValue(BarcodeLengthProperty);
  44. }
  45. set
  46. {
  47. this.SetValue(BarcodeLengthProperty, value);
  48. }
  49. }
  50. public static readonly DependencyProperty MaxScanCountProperty = DependencyProperty.Register(
  51. "MaxScanCount", typeof(int), typeof(AITBarcodeTextBox),
  52. new FrameworkPropertyMetadata(9999, FrameworkPropertyMetadataOptions.AffectsRender));
  53. public int MaxScanCount
  54. {
  55. get
  56. {
  57. return (int)this.GetValue(MaxScanCountProperty);
  58. }
  59. set
  60. {
  61. this.SetValue(MaxScanCountProperty, value);
  62. }
  63. }
  64. public static readonly DependencyProperty BarcodeInputChangedCommandProperty = DependencyProperty.Register(
  65. "BarcodeInputChangedCommand", typeof(ICommand), typeof(AITBarcodeTextBox),
  66. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  67. public ICommand BarcodeInputChangedCommand
  68. {
  69. get
  70. {
  71. return (ICommand)this.GetValue(BarcodeInputChangedCommandProperty);
  72. }
  73. set
  74. {
  75. this.SetValue(BarcodeInputChangedCommandProperty, value);
  76. }
  77. }
  78. //private string _preInput;
  79. public AITBarcodeTextBox()
  80. {
  81. InitializeComponent();
  82. }
  83. private void UIElement_OnPreviewTextInput(object sender, TextCompositionEventArgs e)
  84. {
  85. e.Handled = !IsTextAllowed(e.Text);
  86. }
  87. private static bool IsTextAllowed(string text)
  88. {
  89. return true;
  90. //Regex regex = new Regex("[^0-9.]+"); //regex that matches disallowed text
  91. //return !regex.IsMatch(text);
  92. }
  93. private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
  94. {
  95. TextBox control = (TextBox)sender;
  96. string value = control.Text;
  97. string origin = value;
  98. if (value.Contains(Environment.NewLine))
  99. {
  100. //int pos = value.IndexOf("/");
  101. //string newInput = value.Substring(value.IndexOf("/") + 1, value.IndexOf(Environment.NewLine));
  102. origin = value.Replace(Environment.NewLine, "");
  103. if (!origin.EndsWith("/"))
  104. origin = origin + "/";
  105. if (!string.IsNullOrEmpty(origin))
  106. {
  107. var elements = origin.Split('/');
  108. //if (elements.Length > 0)
  109. // ;
  110. }
  111. }
  112. control.Text = origin;
  113. control.CaretIndex = control.Text.Length;
  114. if (BarcodeInputChangedCommand != null )
  115. {
  116. BarcodeInputChangedCommand.Execute(origin);
  117. }
  118. //string origin = value.Replace("\r\n", "/");
  119. //System.Diagnostics.Trace.WriteLine(value);
  120. //string origin = value.Replace("/", "");
  121. //while (origin.Length > MaxScanCount * BarcodeLength)
  122. //{
  123. // origin = origin.Substring(BarcodeLength);
  124. //}
  125. //if (MaxScanCount > 1 && origin.Length > BarcodeLength)
  126. //{
  127. // string split = "";
  128. // while (origin.Length > BarcodeLength)
  129. // {
  130. // split += origin.Substring(0, BarcodeLength) + "/";
  131. // origin = origin.Substring(BarcodeLength);
  132. // }
  133. // split += origin;
  134. // origin = split;
  135. //}
  136. }
  137. }
  138. }