TEditComboBox.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. 
  2. using System;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Controls.Primitives;
  6. using System.Windows.Input;
  7. namespace FWindSoft.Wpf.Controls
  8. {
  9. public class TEditComboBox:ComboBox
  10. {
  11. private TextBox m_PartTextBox;
  12. public static readonly DependencyProperty TextControlProperty;
  13. static TEditComboBox()
  14. {
  15. TextControlProperty = DependencyProperty.Register("TextControl", typeof(ITextInputControl),
  16. typeof(TEditComboBox));
  17. //InputMethod.IsInputMethodEnabledProperty.OverrideMetadata(typeof(TSZEditComboBox), new FrameworkPropertyMetadata(false));
  18. }
  19. public TEditComboBox()
  20. {
  21. this.AddHandler(TextBoxBase.TextChangedEvent,new RoutedEventHandler(TextBoxBase_OnTextChanged));
  22. DataObject.AddPastingHandler(this, new DataObjectPastingEventHandler(m_DataObjectPastingEventHandler));
  23. }
  24. #region 屏蔽复制命令
  25. public void m_DataObjectPastingEventHandler(object sender, DataObjectPastingEventArgs e)
  26. {
  27. e.CancelCommand();
  28. }
  29. #endregion
  30. #region 文本change
  31. private void TextBoxBase_OnTextChanged(object sender, RoutedEventArgs e)
  32. {
  33. //#region 获取关联textBox
  34. //var element = this.GetSpecifyType<TextBox>();
  35. //if (m_PartTextBox==null&&element.Count > 0)
  36. // m_PartTextBox = element[0];
  37. //#endregion
  38. //TextChangedEventArgs args = e as TextChangedEventArgs;
  39. //if (args == null || this.m_PartTextBox == null)
  40. // return;
  41. //if (TextControl == null)
  42. //{
  43. // return;
  44. //}
  45. //TextChange[] change = new TextChange[args.Changes.Count];
  46. //args.Changes.CopyTo(change, 0);
  47. //int offset = change[0].Offset;
  48. //if (change[0].AddedLength > 0)
  49. //{
  50. // if (!TextControl.InputControl(this.Text))
  51. // {
  52. // this.Text = this.m_OldText;//记录历史值,使用历史值
  53. // m_PartTextBox.Select(offset, 0);
  54. // }
  55. //}
  56. //this.m_OldText = this.Text;
  57. }
  58. #endregion
  59. #region 包装属性
  60. public ITextInputControl TextControl
  61. {
  62. set { SetValue(TextControlProperty, value); }
  63. get { return (ITextInputControl)GetValue(TextControlProperty); }
  64. }
  65. #endregion
  66. protected override void OnPreviewKeyDown(KeyEventArgs e)
  67. {
  68. base.OnKeyDown(e);
  69. if (e.Key == Key.ImeProcessed ||e.Key== Key.Space)
  70. e.Handled = true;
  71. }
  72. protected override void OnPreviewTextInput(TextCompositionEventArgs e)
  73. {
  74. base.OnPreviewTextInput(e);
  75. #region 获取关联textBox
  76. var element = this.GetSpecifyTypeChildren<TextBox>();
  77. if (m_PartTextBox == null && element.Count > 0)
  78. m_PartTextBox = element[0];
  79. #endregion
  80. string strOld = this.Text;
  81. string chaNew = e.Text;
  82. string strFull = string.Format(strOld + chaNew);
  83. if (this.m_PartTextBox != null)
  84. {
  85. strOld = this.m_PartTextBox.Text;
  86. //this.m_PartTextBox.
  87. try
  88. {
  89. string strFront = strOld.Substring(0, m_PartTextBox.SelectionStart);
  90. string strAfter = strOld.Substring(m_PartTextBox.SelectionStart + m_PartTextBox.SelectionLength);
  91. strFull = string.Format(strFront + chaNew + strAfter);//拼接新值
  92. }
  93. catch (ArgumentOutOfRangeException)
  94. {
  95. strOld = strOld + ".";
  96. string strFront = strOld.Substring(0, m_PartTextBox.SelectionStart);
  97. string strAfter = strOld.Substring(m_PartTextBox.SelectionStart + m_PartTextBox.SelectionLength);
  98. strFull = string.Format(strFront + chaNew + strAfter); //拼接新值
  99. }
  100. catch (Exception)
  101. {
  102. }
  103. }
  104. #region 加载验证
  105. if (TextControl != null)
  106. {
  107. e.Handled = !TextControl.InputControl(strFull.Trim());
  108. }
  109. #endregion
  110. }
  111. }
  112. }