wpf 如何使用密码框作为文本框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12517969/
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-13 05:27:24  来源:igfitidea点击:

How to use PasswordBox as TextBox?

c#.netwpftextboxpasswordbox

提问by Nick

I have a PasswordBoxand I need to use this control evenas a TextBox. I need to display normal text and not the typical black dots

我有一个PasswordBox,我需要使用这个控件,甚至作为一个TextBox. 我需要显示普通文本而不是典型的黑点

enter image description here

在此处输入图片说明

Is there a property to do this? Thanks.

有没有物业可以做到这一点?谢谢。

回答by Joshua Van Hoesen

Here is your answer:

这是你的答案:

  1. Use a Textbox
  2. When you want the text masked set TextBox.UseSystemPasswordChar = true;
  3. when you want to see the text set TextBox.UseSystemPasswordChar = false;
  4. Profit
  1. 使用文本框
  2. 当你想要文本屏蔽时设置 TextBox.UseSystemPasswordChar = true;
  3. 当你想看文字时设置TextBox.UseSystemPasswordChar = false;
  4. 利润

Example:

例子:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    if (checkBox1.Checked == true)
    {
        TextBox.UseSystemPasswordChar = true;
    }
    else 
    {
        TextBox.UseSystemPasswordChar = false;
    }
}

Black dots when you want them, words when you don't. You can use what ever trigger/logic you want for the turning on and off but this way you are only using one control and get all the functionality that you specified you needed.

需要时使用黑点,不需要时使用文字。您可以使用您想要的任何触发器/逻辑来打开和关闭,但这样您只需使用一个控件并获得您指定的所有功能。

回答by Bob.

Your best solution would be to have a Password Box with a checkbox underneath that says "Show characters" and then create a Trigger on the Password Box that overlays a TextBox on it for typing and retrieve the text as appropriate.

您最好的解决方案是设置一个密码框,其下方有一个复选框,上面写着“显示字符”,然后在密码框上创建一个触发器,在其上覆盖一个文本框,以便根据需要键入和检索文本。

回答by Manishearth

Superimpose a TextBoxand a PasswordBox, keeping only one Visibleat a time. When you want to switch over, copy over the value of the active one to the other one, and switch their visibilities. Simple.

叠加 aTextBox和 a PasswordBoxVisible一次只保留一个。当你想切换时,将活动一个的值复制到另一个,并切换它们的可见性。简单的。

回答by Damascus

I think everybody agrees on this very basic solution: Use a TextBoxif you want to display what the user is typing.

我认为每个人都同意这个非常基本的解决方案:TextBox如果您想显示用户正在输入的内容,请使用 a 。