TTextBox.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. namespace FWindSoft.Wpf.Controls
  6. {
  7. public class TTextBox : TextBox
  8. {
  9. public static readonly DependencyProperty TextControlProperty;
  10. static TTextBox()
  11. {
  12. TextControlProperty = DependencyProperty.Register("TextControl", typeof (ITextInputControl),
  13. typeof(TTextBox));
  14. //InputMethod.IsInputMethodEnabledProperty.OverrideMetadata(typeof(TSZTextBox),new FrameworkPropertyMetadata(false));
  15. //禁用键盘和粘贴复制
  16. }
  17. public TTextBox()
  18. {
  19. this.UndoLimit = 0;
  20. DataObject.AddPastingHandler(this, new DataObjectPastingEventHandler(m_DataObjectPastingEventHandler));
  21. }
  22. #region 屏蔽复制命令
  23. public void m_DataObjectPastingEventHandler(object sender, DataObjectPastingEventArgs e)
  24. {
  25. e.CancelCommand();
  26. }
  27. #endregion
  28. #region 包装属性
  29. public ITextInputControl TextControl {
  30. set { SetValue(TextControlProperty,value);}
  31. get { return (ITextInputControl)GetValue(TextControlProperty); }
  32. }
  33. #endregion
  34. protected override void OnPreviewKeyDown(KeyEventArgs e)
  35. {
  36. base.OnKeyDown(e);
  37. if (e.Key == Key.ImeProcessed || e.Key == Key.Space)
  38. e.Handled = true;
  39. }
  40. protected override void OnPreviewTextInput(TextCompositionEventArgs e)
  41. {
  42. base.OnPreviewTextInput(e);
  43. string strOld =this.Text;
  44. string chaNew = e.Text;
  45. string strFull = strOld + chaNew;
  46. //12.这样的数字,在1和2中在插入点无法识别。不明原因的末尾小数点舍掉
  47. int charNum = 0;
  48. for (int i = 0; i < this.LineCount; i++)
  49. {
  50. charNum += this.GetLineLength(i);
  51. }
  52. if (charNum > strOld.Length)
  53. {
  54. if (strOld.IndexOf(".") == -1)
  55. {
  56. strOld += string.Format(".");
  57. }
  58. }
  59. while (charNum > strOld.Length)
  60. {
  61. strOld += string.Format("0");
  62. }
  63. try
  64. {
  65. //((System.Windows.Controls.TextBox)(this)).EndPosition
  66. //(typeof(TextBox)).GetProperty("EndPosition", BindingFlags.NonPublic);
  67. //末尾小数点抹掉时可能会出异常
  68. string strFront = strOld.Substring(0, this.SelectionStart);
  69. string strAfter = strOld.Substring(this.SelectionStart + this.SelectionLength);
  70. strFull = string.Format(strFront + chaNew + strAfter); //拼接新值
  71. }
  72. catch (ArgumentOutOfRangeException)
  73. {
  74. //strOld = strOld + ".";
  75. //string strFront = strOld.Substring(0, this.SelectionStart);
  76. //string strAfter = strOld.Substring(this.SelectionStart + this.SelectionLength);
  77. //strFull = string.Format(strFront + chaNew + strAfter); //拼接新值
  78. }
  79. catch (Exception)
  80. {
  81. }
  82. #region 加载验证
  83. if (TextControl != null)
  84. {
  85. e.Handled = !TextControl.InputControl(strFull.Trim());
  86. }
  87. #endregion
  88. }
  89. protected override void OnTextChanged(TextChangedEventArgs e)
  90. {
  91. //if (TextControl == null)
  92. //{
  93. // return;
  94. //}
  95. //TextChange[] change = new TextChange[e.Changes.Count];
  96. //e.Changes.CopyTo(change, 0);
  97. //var dd= this.GetValue(TextProperty);
  98. //string strFull = this.Text;//处理绑定时,最后小数点可能会过滤的情况
  99. //strFull = strFull.Trim();
  100. //int offset = change[0].Offset;
  101. //if (change[0].AddedLength > 0)
  102. //{
  103. // if (!TextControl.InputControl(strFull))
  104. // {
  105. // this.Text = this.m_OldText;//记录历史值,使用历史值
  106. // this.Select(offset, 0);
  107. // }
  108. //}
  109. //this.m_OldText = strFull;
  110. }
  111. }
  112. }