CustomPasswordBox.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Linq;
  3. using System.Windows.Controls;
  4. using System.Windows;
  5. namespace CyberX8_Themes.CustomControls
  6. {
  7. public class CustomPasswordBox : TextBox
  8. {
  9. #region Variables
  10. #region Data
  11. #endregion
  12. public String Password
  13. {
  14. get { return (String)GetValue(PwdTextProperty); }
  15. set
  16. {
  17. SetValue(PwdTextProperty, value);
  18. }
  19. }
  20. /// <summary>
  21. /// The DependencyProperty for the ItemsSource property.
  22. /// Flags: None
  23. /// Default Value: null
  24. /// </summary>
  25. public static readonly DependencyProperty PwdTextProperty
  26. = DependencyProperty.Register("PwdText", typeof(string), typeof(PasswordBox),
  27. new FrameworkPropertyMetadata("",
  28. new PropertyChangedCallback(OnPwdTextChanged)));
  29. private static void OnPwdTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  30. {
  31. PasswordBox ic = (PasswordBox)d;
  32. string oldValue = e.OldValue as string;
  33. string newValue = e.NewValue as string;
  34. }
  35. public String PasswordChar
  36. {
  37. get { return (String)GetValue(PwdCharProperty); }
  38. set
  39. {
  40. SetValue(PwdTextProperty, value);
  41. }
  42. }
  43. /// <summary>
  44. /// The DependencyProperty for the ItemsSource property.
  45. /// Flags: None
  46. /// Default Value: null
  47. /// </summary>
  48. public static readonly DependencyProperty PwdCharProperty
  49. = DependencyProperty.Register("PasswordChar", typeof(String), typeof(PasswordBox),
  50. new FrameworkPropertyMetadata("*",
  51. new PropertyChangedCallback(OnPwdCharChanged)));
  52. private static void OnPwdCharChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  53. {
  54. }
  55. private string SetPwdString(string value)
  56. {
  57. try
  58. {
  59. var str = value + "";
  60. var list = str.ToArray();
  61. var dst = "";
  62. var pwd = "*";
  63. if (PasswordChar != null)
  64. {
  65. pwd = PasswordChar + "";
  66. }
  67. foreach (var item in list)
  68. {
  69. dst += pwd;
  70. }
  71. return dst;
  72. }
  73. catch
  74. {
  75. }
  76. return null;
  77. }
  78. #endregion
  79. #region static Variables
  80. #endregion
  81. #region Methods
  82. #region 构造函数
  83. protected override void OnTextInput(System.Windows.Input.TextCompositionEventArgs e)
  84. {
  85. base.OnTextInput(e);
  86. }
  87. bool ChangeToPwd = false;
  88. protected override void OnTextChanged(TextChangedEventArgs e)
  89. {
  90. var str = SetPwdString(Text);
  91. if (ChangeToPwd)
  92. {
  93. return;
  94. }
  95. ChangeToPwd = true;
  96. int offset = 0;
  97. foreach (var item in e.Changes)
  98. {
  99. offset = item.Offset;
  100. if (item.RemovedLength > 0)
  101. {
  102. Password = Password.Remove(item.Offset, item.RemovedLength);
  103. }
  104. if (item.AddedLength > 0)
  105. {
  106. var insertStr = Text.Substring(item.Offset, item.AddedLength);
  107. Password = Password.Insert(item.Offset, insertStr);
  108. offset = item.Offset + item.AddedLength;
  109. }
  110. }
  111. Text = str;
  112. this.Select(offset, 0);
  113. base.OnTextChanged(e);
  114. ChangeToPwd = false;
  115. }
  116. #endregion
  117. #endregion
  118. #region static Methods
  119. #endregion
  120. #region Event
  121. #endregion
  122. }
  123. }