C#/WPF 在 passwordBox 中取消屏蔽密码

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

C# / WPF Unmask password inside the passwordBox

c#wpfcheckboxpasswordspasswordbox

提问by Benjamin Guino

How could I unmasked and masked the password inside the passwordBox whenever I click the checkBox? I'm using C# WPF template.

每当我单击复选框时,如何取消屏蔽和屏蔽密码框中的密码?我正在使用 C# WPF 模板。

Here is my .XAML code:

这是我的 .XAML 代码:

<PasswordBox x:Name="passwordBox_password" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" Margin="5" Height="25" />
        <CheckBox x:Name="checkBox_showPassword" Grid.Row="3" Grid.Column="1" Margin="5,0,5,5" Content="show password" Checked="checkBox_showPassword_Checked" Unchecked="checkBox_showPassword_Unchecked" />

Here is my .CS code:

这是我的 .CS 代码:

private void checkBox_showPassword_Checked(object sender, RoutedEventArgs e)
    {
        // what to do here ?
    }

    private void checkBox_showPassword_Unchecked(object sender, RoutedEventArgs e)
    {
        // what to do here ?
    }

Or is there another way to do it in WPF?

或者在 WPF 中还有另一种方法吗?

采纳答案by Viralwarrior012

The following link will bring you to the answer you are looking for my good sir. Mr Lamas did a great job of answering the how-to so I'd rather redirect you to the answer :)

以下链接将带您找到您正在寻找我的好先生的答案。喇嘛先生在回答操作方法方面做得很好,所以我宁愿将您重定向到答案:)

showing password characters on some event for passwordbox

在密码框的某些事件上显示密码字符

回答by Marco Concas

It's very simple to do that. First you should to add the value PasswordCharin your PasswordBox:

这样做非常简单。首先,您应该PasswordChar在 PasswordBox 中添加值:

<PasswordBox Name="PasswordHidden" PasswordChar="?"/>

Next under the PasswordBox tag you should to add a TextBox with Visibilityvalue setted to Hidden:

接下来在 PasswordBox 标记下,您应该添加一个Visibility值设置为 Hidden的 TextBox :

<TextBox Name="PasswordUnmask" Visibility="Hidden"/>

And a trigger to show / hide the password, for example a simple text or a button. In my case I'm using a simple text.

以及显示/隐藏密码的触发器,例如简单的文本或按钮。就我而言,我使用的是简单的文本。

<TextBlock Name="ShowPassword"/>

Next you need to add 3 different events in the trigger element, for example (this is valid for TextBlock or Image, if you want to use a Button you should to choose another events):

接下来你需要在触发器元素中添加3个不同的事件,例如(这对TextBlock或Image有效,如果你想使用一个Button你应该选择另一个事件):

<TextBlock x:Name="ShowPassword" Text="SHOW" PreviewMouseDown="ShowPassword_PreviewMouseDown" PreviewMouseUp="ShowPassword_PreviewMouseUp" MouseLeave="ShowPassword_MouseLeave"/>

The events are PreviewMouseDownPreviewMouseUpand MouseLeavebut you can choose the appropriate event for your situation.

这些事件是PreviewMouseDownPreviewMouseUpMouseLeave但您可以根据自己的情况选择适当的事件。

Now in your code you need to program the functions:

现在在您的代码中,您需要对函数进行编程:

private void ShowPassword_PreviewMouseDown(object sender, MouseButtonEventArgs e) => ShowPasswordFunction();
private void ShowPassword_PreviewMouseUp(object sender, MouseButtonEventArgs e) => HidePasswordFunction();
private void ShowPassword_MouseLeave(object sender, MouseEventArgs e) => HidePasswordFunction();

private void ShowPasswordFunction()
{
    ShowPassword.Text = "HIDE";
    PasswordUnmask.Visibility = Visibility.Visible;
    PasswordHidden.Visibility = Visibility.Hidden;
    PasswordUnmask.Text = PasswordHidden.Password;
}

private void HidePasswordFunction()
{
    ShowPassword.Text = "SHOW";
    PasswordUnmask.Visibility = Visibility.Hidden;
    PasswordHidden.Visibility = Visibility.Visible;
}

回答by OMR

I recommend Using MahApps.Metro ... after installing it from nuget.org ... you must use it in the head of your xaml like this xmlns:controls="http://metro.mahapps.com/winf/xaml/controls"

我建议使用 MahApps.Metro ......从 nuget.org 安装它后......你必须像这样在你的 xaml 的头部使用它 xmlns:controls="http://metro.mahapps.com/winf/xaml/控制”

and then ... just use it's style for your PasswordBox control

然后......只需将它的样式用于您的 PasswordBox 控件

<PasswordBox  Style="{StaticResource MetroButtonRevealedPasswordBox}" />

you can even change the content for the show icon using the controls:PasswordBoxHelper.RevealButtonContent attached property

您甚至可以使用控件更改显示图标的内容:PasswordBoxHelper.RevealButtonContent 附加属性