简体   繁体   English

使用文本框作为密码框。 WPF C#

[英]Use a TextBox as a PasswordBox. WPF c#

I have a login form with two textboxes, one for username and one for password.我有一个带有两个文本框的登录表单,一个用于用户名,一个用于密码。

I have used a textbox for the password input, however I would like to mask the characters so that rather than showing letters, it will show a (*) symbol or a password dot.我使用了一个文本框来输入密码,但是我想屏蔽这些字符,而不是显示字母,而是显示 (*) 符号或密码点。

I can not change the textbox to a password box as this throws errors.我无法将文本框更改为密码框,因为这会引发错误。

Using C#, WPF form使用 C#、WPF 形式

I would suggest using the PasswordBox and rewriting your code to work with that instead of coming up with a work around.我建议使用 PasswordBox 并重写您的代码来处理它,而不是想出解决办法。 I am not exactly sure what error you are getting, but you can get the value of the PasswordBox almost the same way as getting the value/text from a TextBox.我不确定您遇到了什么错误,但是您可以通过与从 TextBox 获取值/文本几乎相同的方式获取 PasswordBox 的值。

TextBox:文本框:

TextBox1.Text

PasswordBox:密码框:

PasswordBox1.Password
PasswordBox1.SecurePassword

Try this,尝试这个,

Inside xaml file:-在 xaml 文件中:-

 <TextBlock Height="20" HorizontalAlignment="Right" x:Name ="Pwderrormessage" VerticalAlignment="Top" Width="156" Margin="0,347,60,0" OpacityMask="Crimson" Foreground="#FFE5572C" /> <Label HorizontalContentAlignment="Left" Content="Password:" Foreground="White" TextBlock.FontSize="14" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="230,340,0,0" Height="30" Width="84"/> <StackPanel Height="23" Width="200" Margin="126,170,0,0"> <PasswordBox x:Name="txtpwd" PasswordChar="*" Height="23" Width="200"/> </StackPanel>

And code behind page.和页面背后的代码。

On button click,单击按钮时,

   if (txtpwd.Password.Length == 0)
            {
                Pwderrormessage.Text = "Enter a Password.";
                txtpwd.Focus();
            }

            else
            {
                /*Save you data*/
            }

Thanks, Abhilash.JA谢谢,Abhilash.JA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM