6

我正在开发一个 Windows 手机应用程序。我要求用户登录。

在登录页面上,用户必须输入密码。

现在我想要的是,我给用户一个复选框,后者应该显示密码的字符。

我没有在密码框中看到任何显示密码字符的属性。

请提出一些方法来做到这一点。

4

4 回答 4

23

不要认为 PasswordBox 是可能的......只是一个想法,但您可以使用隐藏的 TextBox 完成相同的结果,当用户单击 CheckBox 时,您只需隐藏 PasswordBox 并显示 TextBox;如果他再次单击,您将再次切换其可见性状态,依此类推...

编辑

这就是方法!

只需添加一个页面,将 ContentPanel 更改为 StackPanel 并添加以下 XAML 代码:

<PasswordBox x:Name="MyPasswordBox" Password="{Binding Text, Mode=TwoWay, ElementName=MyTextBox}"/>
<TextBox x:Name="MyTextBox" Text="{Binding Password, Mode=TwoWay, ElementName=MyPasswordBox}" Visibility="Collapsed" />
<CheckBox x:Name="ShowPasswordCharsCheckBox" Content="Show password" Checked="ShowPasswordCharsCheckBox_Checked" Unchecked="ShowPasswordCharsCheckBox_Unchecked" />

接下来,在页面代码上,添加以下内容:

private void ShowPasswordCharsCheckBox_Checked(object sender, RoutedEventArgs e)
{
    MyPasswordBox.Visibility = System.Windows.Visibility.Collapsed;
    MyTextBox.Visibility = System.Windows.Visibility.Visible;

    MyTextBox.Focus();
}

private void ShowPasswordCharsCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
    MyPasswordBox.Visibility = System.Windows.Visibility.Visible;
    MyTextBox.Visibility = System.Windows.Visibility.Collapsed;

    MyPasswordBox.Focus();
}

这工作得很好,但是再做一些工作,你就可以做到完全 MVVM 了!

于 2012-04-10T16:35:15.813 回答
1

使用默认密码框无法实现您想要的功能。

您可以在这里找到更多信息:http: //social.msdn.microsoft.com/Forums/en/wpf/thread/98d0d4d4-1463-481f-b8b1-711119a6ba99

于 2012-04-10T15:23:31.547 回答
1

我创建了一个 MVVM 示例,我也在现实生活中使用它。请注意 PasswordBox.Password 不是依赖属性,因此不能直接绑定。出于安全原因,它是这样设计的,有关详细信息,请参阅:How to bind to a PasswordBox in MVVM

无论如何,如果你想这样做,你必须使用后面的代码来建立你的视图模型的桥梁。我不提供转换器,因为您可能正在使用自己的一组转换器。如果没有,请向谷歌询问合适的实现。

EnterPasswordWindow.xaml

<Window x:Class="MyDemoApp.Controls.EnterPasswordWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyDemoApp.Controls"
        mc:Ignorable="d" d:DataContext="{d:DesignInstance local:EnterPasswordViewModel}"
        WindowStartupLocation="CenterOwner" ResizeMode="NoResize" SizeToContent="WidthAndHeight"
        Title="Enter Password">
    <StackPanel Margin="4">
        <TextBlock Margin="4">Please enter a password:</TextBlock>
        <TextBox Margin="4" Text="{Binding Password, UpdateSourceTrigger=PropertyChanged}" Visibility="{Binding ShowPassword, Converter={StaticResource BoolToVisibleConverter}}"/>
        <PasswordBox Margin="4" Name="PasswordBox" Visibility="{Binding ShowPassword, Converter={StaticResource BoolToCollapsedConverter}}" PasswordChanged="PasswordBox_PasswordChanged"/>
        <CheckBox Margin="4" IsChecked="{Binding ShowPassword}">Show password</CheckBox>
        <DockPanel>
            <Button Margin="4" Width="150" Height="30" IsDefault="True" IsEnabled="{Binding Password, Converter={StaticResource StringIsNotNullOrEmptyConverter}}" Click="Button_Click">OK</Button>
            <Button Margin="4" Width="150" Height="30" IsCancel="True" HorizontalAlignment="Right">Cancel</Button>
        </DockPanel>
    </StackPanel>
</Window>

EnterPasswordWindow.xaml.cs

using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace MyDemoApp.Controls
{
    /// <summary>
    /// Interaction logic for EnterPasswordWindow.xaml
    /// </summary>
    public partial class EnterPasswordWindow : Window
    {
        public EnterPasswordWindow()
        {
            InitializeComponent();
            DataContext = ViewModel = new EnterPasswordViewModel();
            ViewModel.PropertyChanged += ViewModel_PropertyChanged;
        }

        public EnterPasswordViewModel ViewModel { get; set; }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }

        private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (!mSuppressPropertyChangedEvent && e.PropertyName == nameof(ViewModel.Password))
            {
                PasswordBox.Password = ViewModel.Password;
            }
        }
        private bool mSuppressPropertyChangedEvent;
        private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            mSuppressPropertyChangedEvent = true;
            ViewModel.Password = ((PasswordBox)sender).Password;
            mSuppressPropertyChangedEvent = false;
        }
    }
}

EnterPasswordViewModel.cs

using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace MyDemoApp.Controls
{
    public class EnterPasswordViewModel : INotifyPropertyChanged
    {
        public string Password
        {
            get => mPassword;
            set
            {
                if (mPassword != value)
                {
                    mPassword = value;
                    NotifyPropertyChanged();
                }
            }
        }
        private string mPassword;

        public bool ShowPassword
        {
            get => mShowPassword;
            set
            {
                if (mShowPassword != value)
                {
                    mShowPassword = value;
                    NotifyPropertyChanged();
                }
            }
        }
        private bool mShowPassword;

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName]string propertyName = null)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                return;
            }
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
于 2020-07-01T09:36:58.480 回答
0

您可以创建自己的控件,该控件继承自文本框,但是在每个字符之后将其替换为 *,将真实值存储在页面上的私有变量中。然后,您可以使用复选框切换文本框中的值是显示真值还是 * 值。

这不是一个优雅的解决方案,也不是最佳实践,但我认为如果你愿意接受它,它仍然是一种选择。

于 2012-04-10T16:35:50.990 回答