我想知道有人知道如何在 PasswordBox 中显示密码。已阅读您可以将文本框绑定到密码框,但这不是另一种方法。
问问题
10615 次
2 回答
5
如果您希望您的密码可见,您应该使用文本框。密码框的唯一功能是屏蔽输入。它不提供额外的功能。也可以在运行时切换文本框和密码框
于 2013-06-09T08:06:50.997 回答
0
PasswordBox 控件中没有任何内置属性可以显示密码字符。但是我们可以通过 TextBox 控件在 PasswordBox 中显示 Password 来做到这一点。对于在 WPF 中具有显示/隐藏功能的 PasswordBox。我们将使用两个TextBlock控件,TextBox控件,PasswordBox控件,Image控件
XAML
<Grid>
<TextBlock Text="Welcome
To see the Password"
FontSize="28"
FontWeight="Bold"
HorizontalAlignment="Center"
VerticalAlignment="Top"
TextAlignment="Center"
Margin="0,30,0,0" />
<TextBlock Text="Enter your password"
FontSize="20"
HorizontalAlignment="Left"
Margin="112,160,0,0"
VerticalAlignment="Top" />
<TextBox Height="40"
FontSize="20"
Padding="5,2,47,0"
HorizontalAlignment="Left"
Margin="112,193,0,0"
Name="txtVisiblePasswordbox"
VerticalAlignment="Top"
Width="274" />
<PasswordBox Height="40"
FontSize="20"
Padding="5,2,47,0"
HorizontalAlignment="Left"
Margin="112,193,0,0"
Name="txtPasswordbox"
VerticalAlignment="Top"
Width="274"
PasswordChanged="txtPasswordbox_PasswordChanged" />
<Image Visibility="Hidden"
Height="30"
HorizontalAlignment="Left"
Name="ImgShowHide"
Stretch="Fill"
VerticalAlignment="Top"
Width="30"
Margin="351,198,0,0"
MouseLeave="ImgShowHide_MouseLeave"
PreviewMouseDown="ImgShowHide_PreviewMouseDown"
PreviewMouseUp="ImgShowHide_PreviewMouseUp" />
</Grid>
代码背后
string AppPath = Directory.GetCurrentDirectory();
public MainWindow()
{
InitializeComponent();
ImgShowHide.Source = new BitmapImage(new Uri(AppPath + "\\img\\clip.jpg"));
}
private void ImgShowHide_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
HidePassword();
}
private void ImgShowHide_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
ShowPassword();
}
private void ImgShowHide_MouseLeave(object sender, MouseEventArgs e)
{
HidePassword();
}
private void txtPasswordbox_PasswordChanged(object sender, RoutedEventArgs e)
{
if(txtPasswordbox.Password.Length > 0)
ImgShowHide.Visibility = Visibility.Visible;
else
ImgShowHide.Visibility = Visibility.Hidden;
}
void ShowPassword()
{
ImgShowHide.Source = new BitmapImage(new Uri(AppPath + "\\img\\cus.jpg"));
txtVisiblePasswordbox.Visibility = Visibility.Visible;
txtPasswordbox.Visibility = Visibility.Hidden;
txtVisiblePasswordbox.Text = txtPasswordbox.Password;
}
void HidePassword()
{
ImgShowHide.Source = new BitmapImage(new Uri(AppPath + "\\img\\clip.jpg"));
txtVisiblePasswordbox.Visibility = Visibility.Hidden;
txtPasswordbox.Visibility = Visibility.Visible;
txtPasswordbox.Focus();
}
于 2015-09-23T09:53:16.793 回答