简体   繁体   English

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

[英]C# / WPF Unmask password inside the passwordBox

How could I unmasked and masked the password inside the passwordBox whenever I click the checkBox? 每当我点击checkBox时,我怎么能解密并屏蔽passwordBox中的密码? 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中有另一种方法吗?

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 :) Lamas先生在回答方法方面做得很好,所以我宁愿把你重定向到答案:)

showing password characters on some event for passwordbox 在密码箱的某些事件上显示密码字符

It's very simple to do that. 这样做非常简单。 First you should to add the value PasswordChar in your PasswordBox: 首先,您应该在PasswordChar中添加值PasswordChar:

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

Next under the PasswordBox tag you should to add a TextBox with Visibility value setted to Hidden: 在PasswordBox标记下,您应该添加一个Textibility,其Visibility值设置为Hidden:

<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): 接下来,您需要在trigger元素中添加3个不同的事件,例如(这对TextBlock或Image有效,如果您想使用Button,则应选择其他事件):

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

The events are PreviewMouseDown PreviewMouseUp and MouseLeave but you can choose the appropriate event for your situation. 事件是PreviewMouseDown PreviewMouseUpMouseLeave但您可以根据自己的情况选择适当的事件。

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;
}

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 您甚至可以使用控件更改show图标的内容:PasswordBoxHelper.RevealButtonContent附加属性

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

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