WPF密碼如何綁定在密碼框中
WPF開發(fā)工具在實(shí)際使用中有很多功能的實(shí)現(xiàn),需要我們?nèi)ド钊氲奶接憽1热邕@篇文章為大家介紹有關(guān)WPF密碼的一些應(yīng)用方法等。#t#
正如綁定TextBox控件的Text屬性一樣, 我們希望能夠?qū)asswordBox空間的WPF密碼Password屬性進(jìn)行綁定, 比如在MVVM模式中,這似乎是必須的, 但可惜的是, Password屬性是不支持綁定的(不是依賴屬性, 也沒有實(shí)現(xiàn)INotifyPropertyChanged).
這可能是出于安全性的考慮. 但在我們的系統(tǒng)為了實(shí)現(xiàn)View層密碼框中的密碼與后臺(tái)其它層之間的密碼屬性之間的綁定, 可以采取如下思路: 將密碼框的密碼和某一個(gè)緩沖區(qū)進(jìn)行同步, 緩沖區(qū)在和后臺(tái)進(jìn)行綁定. 其中密碼框與緩沖區(qū)之間的同步可采用事件進(jìn)行通知, 并將緩沖區(qū)打造成依賴屬性, 然后緩沖區(qū)就支持綁定了, 并給后臺(tái)提供正確的密碼.
緩沖區(qū)可以是哈希表或其他字典結(jié)構(gòu), 以便將WPF密碼框和緩沖區(qū)中的密碼一 一對(duì)應(yīng)起來, 也可以使AttachProperty(附加屬性), 其實(shí)附加屬性的機(jī)制也就是對(duì)緩存了的一個(gè)大字典進(jìn)行操作
- 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密碼后, 需要手動(dòng)更新密碼框插入符(CaretIndex)的位置, 可惜的是, 密碼框并沒有給我們提供這樣的屬性或方法(TextBox有, PasswordBox沒有), 可以采用下面的方法來設(shè)置:
- 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 });- }