WPF中PasswordBox控件怎么显示提示信息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF中PasswordBox控件怎么显示提示信息相关的知识,希望对你有一定的参考价值。

参考技术A 没做过这个东西.但是js可以实现吧. 就是在 keyup事件中,如果文本框的值是请输入密码的时候,那么就给这个文本框 的 type属性值改成 text. 如果文本框的值 请输入密码 ,那么文本框的type属性值就改为password .本回答被提问者采纳

WPF PasswordBox password数据绑定

WPF的PasswordBox控件的Password属性不是依赖属性,无法直接进行数据绑定,为使其在MVVM模式中正常使用,可以为PasswordBox增加一个助手类,步骤如下:

原文链接:http://blog.csdn.net/ryb666666/article/details/7629767

1.新建一个PasswordBoxHelper 帮助类 

技术分享
 1  public static class PasswordBoxHelper
 2     {
 3         public static readonly DependencyProperty PasswordProperty =
 4             DependencyProperty.RegisterAttached("Password",
 5             typeof(string), typeof(PasswordBoxHelper),
 6             new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));
 7         public static readonly DependencyProperty AttachProperty =
 8             DependencyProperty.RegisterAttached("Attach",
 9             typeof(bool), typeof(PasswordBoxHelper), new PropertyMetadata(false, Attach));
10         private static readonly DependencyProperty IsUpdatingProperty =
11            DependencyProperty.RegisterAttached("IsUpdating", typeof(bool),
12            typeof(PasswordBoxHelper));
13 
14         public static void SetAttach(DependencyObject dp, bool value)
15         {
16             dp.SetValue(AttachProperty, value);
17         }
18         public static bool GetAttach(DependencyObject dp)
19         {
20             return (bool)dp.GetValue(AttachProperty);
21         }
22         public static string GetPassword(DependencyObject dp)
23         {
24             return (string)dp.GetValue(PasswordProperty);
25         }
26         public static void SetPassword(DependencyObject dp, string value)
27         {
28             dp.SetValue(PasswordProperty, value);
29         }
30         private static bool GetIsUpdating(DependencyObject dp)
31         {
32             return (bool)dp.GetValue(IsUpdatingProperty);
33         }
34         private static void SetIsUpdating(DependencyObject dp, bool value)
35         {
36             dp.SetValue(IsUpdatingProperty, value);
37         }
38         private static void OnPasswordPropertyChanged(DependencyObject sender,
39             DependencyPropertyChangedEventArgs e)
40         {
41             PasswordBox passwordBox = sender as PasswordBox;
42             passwordBox.PasswordChanged -= PasswordChanged;
43             if (!(bool)GetIsUpdating(passwordBox))
44             {
45                 passwordBox.Password = (string)e.NewValue;
46             }
47             passwordBox.PasswordChanged += PasswordChanged;
48         }
49         private static void Attach(DependencyObject sender,
50             DependencyPropertyChangedEventArgs e)
51         {
52             PasswordBox passwordBox = sender as PasswordBox;
53             if (passwordBox == null)
54                 return;
55             if ((bool)e.OldValue)
56             {
57                 passwordBox.PasswordChanged -= PasswordChanged;
58             }
59             if ((bool)e.NewValue)
60             {
61                 passwordBox.PasswordChanged += PasswordChanged;
62             }
63         }
64         private static void PasswordChanged(object sender, RoutedEventArgs e)
65         {
66             PasswordBox passwordBox = sender as PasswordBox;
67             SetIsUpdating(passwordBox, true);
68             SetPassword(passwordBox, passwordBox.Password);
69             SetIsUpdating(passwordBox, false);
70         }
71     }
View Code

2.在View层XAML绑定

1  <PasswordBox x:Name="passwordBox"  PasswordChar="*" Helper:PasswordBoxHelper.Attach="True"
2              Helper:PasswordBoxHelper.Password="{Binding Path=Pwd,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
1 <Window x:Class="HR_Test01.View.LoginView"
2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4         xmlns:Helper="clr-namespace:HR_Test01.Model"        
5         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7         xmlns:local="clr-namespace:HR_Test01.View"
8         mc:Ignorable="d"  AllowsTransparency="True" WindowStyle="None"  MouseLeftButtonDown="Window_MouseLeftButtonDown" 
9         Title="登录界面" Height="250" Width="400" WindowStartupLocation="CenterScreen">

 

以上是关于WPF中PasswordBox控件怎么显示提示信息的主要内容,如果未能解决你的问题,请参考以下文章

2021-08-09 WPF控件专题 Label,TextBox,PasswordBox,Button控件介绍

WPF中PasswordBox控件的Password属性的数据绑定

如何在WPF PasswordBox中设置一些默认的文本

WPF 在MVVM模式下怎样在Viewmodel里面控件与view中控件相关联。

由一次PasswordBox密码绑定引发的疑问 ---; WPF中的附加属性的定义,以及使用。

WPF PasswordBox password数据绑定