WPF TextBox模仿PasswordBox的密码显示功能
WPF TextBox模仿PasswordBox的密码显示功能
这并不是多此一举,因为WPF的PasswordBox不支持继承,所以想扩展PasswordBox的属性就没法实现,所以有了本文内容,当然这个思路也可以扩展到其他语言。
已经更新了升级版,链接如下升级版链接
https://blog.csdn.net/qq_41908152/article/details/122067744
一、添加属性 Password,用于存储密码
public string Password { get; set; } = string.Empty;
二、添加属性 PasswordChar,用于设置显示为密码的字符,默认为 ‘●’
public char PasswordChar { get; set; } = '●';
三、重写TextBox的TextChanged事件事件,代码以及内部逻辑如下(用户有可能往密码框里粘贴密码,以下代码已全部考虑到了此操作)
//重写文本框内容改变事件protected override void OnTextChanged(TextChangedEventArgs e){base.OnTextChanged(e);//已键入的文本长度 Text 为 TextBox 的属性(获取或设置文本框的文本内容)int textLength = Text.Length;//已保存的密码长度int psdLength = Password.Length;//起始修改位置int startIndex = -1;for (int i = 0; i < textLength; i++){if (Text[i] != PasswordChar){startIndex = i;break;}}//未作任何修改if (startIndex == -1 && textLength == psdLength) return;//结束修改位置int stopIndex = -1;for (int i = textLength - 1; i >= 0; i--){if (Text[i] != PasswordChar){stopIndex = i;break;}}//添加或修改了一个或连续的多个值if (startIndex != -1){//累计修改长度int alterLength = stopIndex - startIndex + 1;//长度没变化,单纯的修改了一个或连续的多个值if (textLength == psdLength){Password = Password.Substring(0, startIndex) + Text.Substring(startIndex, alterLength) + Password.Substring(stopIndex + 1);}//新增了内容else{//新增且修改了原来内容if (alterLength > textLength - psdLength){//计算未修改密码个数 textLength - alterLength//计算已修改密码个数 = 原密码长度 - 未修改密码个数 psdLength - (textLength - alterLength)//原密码该保留的后半部分的索引 = 已修改密码个数 + 起始修改位置Password = Password.Substring(0, startIndex) + Text.Substring(startIndex, alterLength) + Password.Substring(psdLength - (textLength - alterLength) + startIndex);}//单纯的新增了一个或多个连续的值else{Password = Password.Substring(0, startIndex) + Text.Substring(startIndex, alterLength) + Password.Substring(startIndex);}}}//删除了一个或连续的多个值else{//已删除的数据长度 SelectionStart 为 TextBox 的属性(获取或设置当前所选内容的起始位置的字符索引)int length = psdLength - textLength;if (SelectionStart < textLength){//改变了中间的数据Password = Password.Substring(0, SelectionStart) + Password.Substring(SelectionStart + length);}else{//删除了结尾的数据Password = Password.Substring(0, SelectionStart);}}//记住光标位置(设置完Text后会丢失,所以现在要记住)int selectionStart = SelectionStart;//设置显示密码Text = new string(PasswordChar, textLength);//设置光标位置SelectionStart = selectionStart;}
怕有些初学者迷茫,把完整的类也贴出来吧(这是创建了一个“自定义控件”),虽然没啥东西(包含上述代码)
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace SuperControl
{public class SuperPasswordBox : TextBox{static SuperPasswordBox(){DefaultStyleKeyProperty.OverrideMetadata(typeof(SuperPasswordBox), new FrameworkPropertyMetadata(typeof(TextBox)));}/// /// 密码/// public string Password { get; set; } = string.Empty;/// /// 显示为密码的字符/// public char PasswordChar { get; set; } = '●';//重写文本框内容改变事件protected override void OnTextChanged(TextChangedEventArgs e){base.OnTextChanged(e);//已键入的文本长度int textLength = Text.Length;//已保存的密码长度int psdLength = Password.Length;//起始修改位置int startIndex = -1;for (int i = 0; i < textLength; i++){if (Text[i] != PasswordChar){startIndex = i;break;}}//未作任何修改if (startIndex == -1 && textLength == psdLength) return;//结束修改位置int stopIndex = -1;for (int i = textLength - 1; i >= 0; i--){if (Text[i] != PasswordChar){stopIndex = i;break;}}//添加或修改了一个或连续的多个值if (startIndex != -1){//累计修改长度int alterLength = stopIndex - startIndex + 1;//长度没变化,单纯的修改了一个或连续的多个值if (textLength == psdLength){Password = Password.Substring(0, startIndex) + Text.Substring(startIndex, alterLength) + Password.Substring(stopIndex + 1);}//新增了内容else{//新增且修改了原来内容if (alterLength > textLength - psdLength){//计算未修改密码个数 textLength - alterLength//计算已修改密码个数 = 原密码长度 - 未修改密码个数 psdLength - (textLength - alterLength)//原密码该保留的后半部分的索引 = 已修改密码个数 + 起始修改位置Password = Password.Substring(0, startIndex) + Text.Substring(startIndex, alterLength) + Password.Substring(psdLength - (textLength - alterLength) + startIndex);}//单纯的新增了一个或多个连续的值else{Password = Password.Substring(0, startIndex) + Text.Substring(startIndex, alterLength) + Password.Substring(startIndex);}}}//删除了一个或连续的多个值else{//已删除的数据长度int length = psdLength - textLength;if (SelectionStart < textLength){//改变了中间的数据Password = Password.Substring(0, SelectionStart) + Password.Substring(SelectionStart + length);}else{//删除了结尾的数据Password = Password.Substring(0, SelectionStart);}}//记住光标位置(设置完Text后会丢失,所以现在要记住)int selectionStart = SelectionStart;//设置显示密码Text = new string(PasswordChar, textLength);//设置光标位置SelectionStart = selectionStart;}}
}
有问题欢迎留言,如果觉得有用请点个"赞",谢谢!
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
