WPF中无法绑定PasswordBox的Password问题
由于PasswordBox的Password不是依赖属性,所以无法对其进行绑定。
这是需要通过添加附加属性,在附加属性中通过PasswordBox中可以用的依赖属性关联,实现Password绑定。
依赖属性:
-----MonitorPassword:关联PasswordBox中的PasswordChange事件。当PasswordChange事件发生时,修改AttachPassword属性。
-----AttachPassword:被绑定的附加属性
-----HasText:是否显示水印附加属性
附加属性类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | public class PasswordProperties { #region 绑定PasswordBox.PasswordChange事件,一旦发生passwordChange事件,设置其余两个附件属性; public static bool GetMonitorPassword(DependencyObject obj) { return ( bool )obj.GetValue(MonitorPasswordProperty); } public static void SetMonitorPassword(DependencyObject obj, bool value) { obj.SetValue(MonitorPasswordProperty, value); } // Using a DependencyProperty as the backing store for MonitorPassword. This enables animation, styling, binding, etc... public static readonly DependencyProperty MonitorPasswordProperty = DependencyProperty.RegisterAttached( "MonitorPassword" , typeof ( bool ), typeof (PasswordProperties), new PropertyMetadata( false , OnMonitorPasswordChanged)); private static void OnMonitorPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { PasswordBox? p = d as PasswordBox; if (p == null ) return ; p.PasswordChanged -= P_PasswordChanged; if (( bool )e.NewValue) { SetHasText(p); p.PasswordChanged += P_PasswordChanged; } } private static void P_PasswordChanged( object sender, RoutedEventArgs e) { PasswordBox? p = sender as PasswordBox; if (p == null ) return ; SetHasText(p); SetAttachPassword(p); } #endregion #region password的附件属性 public static string GetAttachPassword(PasswordBox obj) { return ( string )obj.GetValue(AttachPasswordProperty); } public static void SetAttachPassword(PasswordBox obj) { obj.SetValue(AttachPasswordProperty, obj.Password); } // Using a DependencyProperty as the backing store for AttachPassword. This enables animation, styling, binding, etc... public static readonly DependencyProperty AttachPasswordProperty = DependencyProperty.RegisterAttached( "AttachPassword" , typeof ( string ), typeof (PasswordProperties), new PropertyMetadata(0)); #endregion #region password是否为空的附件属性,用于判断是否显示水印 public static bool GetHasText(PasswordBox obj) { return ( bool )obj.GetValue(HasTextProperty); } public static void SetHasText(PasswordBox obj) { obj.SetValue(HasTextProperty, obj.Password.Length > 0); } // Using a DependencyProperty as the backing store for HasText. This enables animation, styling, binding, etc... public static readonly DependencyProperty HasTextProperty = DependencyProperty.RegisterAttached( "HasText" , typeof ( bool ), typeof (PasswordProperties), new PropertyMetadata( false )); #endregion } |
xmal: 绑定viewmodel中的Password属性
1 | < PasswordBox local:PasswordProperties.MonitorPassword="True" local:PasswordProperties.AttachPassword="{Binding Password}" /> |
分类:
C#
posted on 2022-09-13 11:48 xzj19870125 阅读(338) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!