如何在WPF密码框中显示几秒钟的字符?
如果用户在密码框中输入1985,则会显示四个项目符号(●●●●)。如何显示输入的每个字母或数字几秒钟,然后将其更改为项目符号?我想这不能在密码框中完成,但是还有其他方法吗?
If the user enters 1985 in the password box, four bullets (●●●●) will be shown. How can I show each letter or number entered for a few seconds and after that change it to a bullet? I suppose that this can't be done in the password box, but is there any other way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将文本框放在密码框的顶部,然后使用一些数据绑定和动画。只要正在进行输入,该 XAML 块就允许文本框可见,但一旦停止输入,文本框就会消失,只留下显示密码字符的密码框。
您可以调整动画中的关键时间以获得您喜欢的延迟。您还可以更改文本框中的字体设置,以使键入的文本和密码字符更好地对齐。
编辑
如果您只想显示以明文形式输入的最后一个字符:
这种情况有点不同,并且需要更多的复杂性。此场景仅使用窗口上的文本框,而不使用密码框。
在窗口的代码隐藏中(或在 ViewModel 类中),您将需要两个属性:
ActualPwd
和DisplayedPwd
。该文本框绑定到DisplayedPwd
属性。在代码隐藏中,您将需要以下代码:
tbxPwd_PreviewTextInput 方法用于检索用户键入的字符。 tbxPwd_PreviewKeyDown 方法用于检索 BackSpace 键或您想要检测的任何其他控制字符键。
此代码没有延迟,因此它始终以明文形式显示密码字符串的最后一个字符。添加一些代码和计时器以在一段延迟后将最后一个字符更改为 pwd 字符应该很容易。
上面的代码尚未经过彻底调试,因此如果用户将整个密码退格以重新开始,可能会出现问题。
提示:Alt+0149 显示“bullet”密码字符。
Put a textbox on top of the password box, and then use a little databinding and animation. This chunk of XAML will allow the textbox to be visible as long as there is typing going on, but as soon as the typing stops, the textbox will fade away, leaving only the password box with the password characters showing.
You can play around with the KeyTimes in the animation to get the delay that you prefer. You can also change the font settings in the textbox to get the typed text and the password characters to line up better.
EDIT
If you want to only display the last character typed as clear text:
This situation is a little different and requires some more complexity. this scenario uses only a textbox on the window, not the passwordbox.
In your code-behind for the window (or in your ViewModel class) you will need two properties,
ActualPwd
andDisplayedPwd
. The textbox is bound to theDisplayedPwd
property.In the code-behind, you will need the following code:
The tbxPwd_PreviewTextInput method is used to retrieve the character that is typed by the user. The tbxPwd_PreviewKeyDown method is used to retrieve the BackSpace key, or any other control character key you want to detect.
There is no delay in this code, so it always displays the last character of the password string in clear text. It should be easy enough to add some code along with a Timer to change the last character to the pwd character after some delay.
The above code hasn't been thoroughly debugged, so there may be issues if the user backspaces out their entire password to start over.
Tip: Alt+0149 displays the 'bullet' password character.
只需使用一个
TextBox
即可实现这一目标。请参阅下面的代码:窗口的 XAML 代码:
C# 隐藏代码:
从 地图类型
This can be achieved using just one
TextBox
. See the code below:XAML code for window:
C# Code behind:
MapType
enum
retrieved from MapType我已经实现了类似的功能,在这里作为一个更简单的示例,密码框替换为文本框,以在鼠标悬停时显示密码密码框,如下所示
的 xaml 代码
用于窗口C# 代码
另外,如果您正在开发 mvp 或 mvp-vm wpf 应用程序,请不要忘记将传入的值绑定到后面代码中的 LicensePasswordBox 和 LicensePasswordTextBox
I have implemented similar functionality leaving here as a simpler example, passwordbox replaced by textbox to show password when mouse hovers password box as follows
xaml code for window
c# code behind
Also if you are developing an mvp or mvp-vm wpf application, do not forget to bind coming value to both LicencePasswordBox and LicencePasswordTextBox in code behind