如何让 WPF TextBox 使用密码字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3792764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:09:08  来源:igfitidea点击:

How to make a WPF TextBox use password characters?

wpfvb.netwinformstextboxpasswords

提问by pvaju896

i need to set it it dynamicaliy..

我需要动态设置它..

Can i make password Box to as normal text- i mean- user could see the text what he entered.???

我可以将密码框设为普通文本 - 我的意思是 - 用户可以看到他输入的文本。???

its for-> i need to use same control for " password sesion" and also the "item count entering" session ..??

它的->我需要对“密码会话”和“项目计数输入”会话使用相同的控件..??

回答by Kishore Kumar

You have to use PasswordBoxinstead of TextBox:

你必须使用PasswordBox而不是TextBox

<PasswordBox Height="42" Width="200"  Margin="22,28,28,0" 
         Name="passwordBox1" VerticalAlignment="Top" 
         Background="LightBlue" Foreground="DarkBlue" 
         MaxLength="25" PasswordChar="*" 
         />

回答by Will

There's a new control in WPF designed for passwords, it's called PasswordBox. You should use that instead of a TextBoxif you need to mask the input.

WPF 中有一个专为密码设计的新控件,称为PasswordBox. TextBox如果您需要屏蔽输入,您应该使用它而不是 a 。

Here's a brief article about it.To retrieve the value that was entered, use the Passwordproperty.

这是一篇关于它的简短文章。要检索输入的值,请使用Password属性。

EDIT: You've pretty much asked a new question - how can you unmask the text in a WPF PasswordBox? To the best of my knowledge you can't, though you could of course display it in a regular TextBox on demand by getting the value of the password from PasswordBox.Password

编辑:您几乎问了一个新问题 -如何在 WPF PasswordBox 中取消屏蔽文本?据我所知,您不能,尽管您当然可以通过从PasswordBox.Password

Databinding to a PasswordBoxisn't possible without a custom helper class - though this would reduce the increased security offered by the new PasswordBoxcontrol (as described here). With that considered, this article includes a section on creating a helper classthat allows you to databind to a PasswordBox.

PasswordBox如果没有自定义帮助程序类,数据绑定到 a是不可能的 - 尽管这会降低新PasswordBox控件提供的增加的安全性(如此处所述)。考虑到这一点,本文包含一个关于创建帮助类的部分,该类允许您将数据绑定到PasswordBox.

回答by Tommi Rouvali

Or use 'password font'. A font that shows all characters as dots. Remember to disable cut and copy clipboard functions.

或使用“密码字体”。一种将所有字符显示为点的字体。请记住禁用剪切和复制剪贴板功能。

回答by falstaff

There are also custom implementations of PasswordBoxwhich you can alter for your needs, see this article. You also can extend this which is not possible with the PasswordBoxbecause its a sealed class.

PasswordBox您还可以根据需要更改自定义实现,请参阅 本文。您也可以扩展它,这是不可能的,PasswordBox因为它是一个密封类。