WPF开发工具在实际使用中有很多功能的实现,需要我们去深入的探讨。比如这篇文章为大家介绍有关WPF密码的一些应用方法等。#t#

创新互联专业提供绵阳主机托管服务,为用户提供五星数据中心、电信、双线接入解决方案,用户可自行在线购买绵阳主机托管服务,并享受7*24小时金牌售后服务。
正如绑定TextBox控件的Text属性一样, 我们希望能够将PasswordBox空间的WPF密码Password属性进行绑定, 比如在MVVM模式中,这似乎是必须的, 但可惜的是, Password属性是不支持绑定的(不是依赖属性, 也没有实现INotifyPropertyChanged).
这可能是出于安全性的考虑. 但在我们的系统为了实现View层密码框中的密码与后台其它层之间的密码属性之间的绑定, 可以采取如下思路: 将密码框的密码和某一个缓冲区进行同步, 缓冲区在和后台进行绑定. 其中密码框与缓冲区之间的同步可采用事件进行通知, 并将缓冲区打造成依赖属性, 然后缓冲区就支持绑定了, 并给后台提供正确的密码.
缓冲区可以是哈希表或其他字典结构, 以便将WPF密码框和缓冲区中的密码一 一对应起来, 也可以使AttachProperty(附加属性), 其实附加属性的机制也就是对缓存了的一个大字典进行操作
- public static class Password
 
BoxBindingHelper- {
 - public static bool GetIsPassword
 
BindingEnabled(DependencyObject obj)- {
 - return (bool)obj.GetValue
 
(IsPasswordBindingEnabledProperty);- }
 - public static void SetIsPassword
 
BindingEnabled(DependencyObject
obj, bool value)- {
 - obj.SetValue(IsPasswordBinding
 
EnabledProperty, value);- }
 - public static readonly Dependency
 
Property IsPasswordBinding
EnabledProperty =- DependencyProperty.Register
 
Attached("IsPasswordBinding
Enabled", typeof(bool),- typeof(PasswordBoxBindingHelper),
 - new UIPropertyMetadata
 
(false, OnIsPasswordBinding
EnabledChanged));- private static void
 
OnIsPasswordBindingEnabled
Changed(DependencyObject obj,- DependencyPropertyChangedEventArgs e)
 - {
 - var passwordBox = obj as
 
PasswordBox;- if(passwordBox != null)
 - {
 - passwordBox.PasswordChanged
 
-= PasswordBoxPasswordChanged;- if ((bool)e.NewValue)
 - {
 - passwordBox.PasswordChanged
 
+= PasswordBoxPasswordChanged;- }
 - }
 - }
 - //when the passwordBox's password
 
changed, update the buffer- static void PasswordBoxPassword
 
Changed(object sender, RoutedEventArgs e)- {
 - var passwordBox = (PasswordBox) sender;
 - if (!String.Equals(GetBindedPassword
 
(passwordBox),passwordBox.Password))- {
 - SetBindedPassword(passwordBox,
 
passwordBox.Password);- }
 - }
 - public static string GetBindedPassword
 
(DependencyObject obj)- {
 - return (string)obj.GetValue
 
(BindedPasswordProperty);- }
 - public static void SetBindedPassword
 
(DependencyObject obj, string value)- {
 - obj.SetValue(BindedPasswordProperty, value);
 - }
 - public static readonly Dependency
 
Property BindedPasswordProperty =- DependencyProperty.RegisterAttached
 
("BindedPassword", typeof(string),- typeof(PasswordBoxBindingHelper),
 - new UIPropertyMetadata(string.Empty,
 
OnBindedPasswordChanged));- //when the buffer changed, upate
 
the passwordBox's password- private static void OnBindedPassword
 
Changed(DependencyObject obj,- DependencyPropertyChangedEventArgs e)
 - {
 - var passwordBox = obj as PasswordBox;
 - if (passwordBox != null)
 - {
 - passwordBox.Password = e.NewValue ==
 
null ? string.Empty : e.NewValue.ToString();- }
 - }
 - }
 
在View层, 如下使用便可以了:
- < PasswordBox Helpers:PasswordBox
 
BindingHelper.IsPassword
BindingEnabled="True"- Helpers:PasswordBoxBinding
 
Helper.BindedPassword=- "{Binding Path=Password,
 
Mode=TwoWay, UpdateSource
Trigger=PropertyChanged}" />
另外, 在更改了密码框的WPF密码后, 需要手动更新密码框插入符(CaretIndex)的位置, 可惜的是, 密码框并没有给我们提供这样的属性或方法(TextBox有, PasswordBox没有), 可以采用下面的方法来设置:
- private static void SetPassword
 
BoxSelection(PasswordBox
passwordBox, int start, int length)- {
 - var select = passwordBox.
 
GetType().GetMethod("Select",- BindingFlags.Instance |
 
BindingFlags.NonPublic);- select.Invoke(passwordBox,
 
new object[] { start, length });- }
 
                当前名称:WPF密码如何绑定在密码框中
                
                网页网址:http://www.csdahua.cn/qtweb/news31/226231.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网