Friday, August 12, 2016

Databind between View and ViewModel for PasswordBox WPF

"When you try to databind the password property of a PasswordBox you will recognize that you cannot do data binding on it. The reason for this is, that the password property is not backed by a DependencyProperty.

The reason is databinding passwords is not a good design for security reasons and should be avoided. But sometimes this security is not necessary, then it's only cumbersome that you cannot bind to the password property. In this special cases you can take advantage of the following PasswortBoxHelper.

The PasswordHelper is attached to the password box by calling the PasswordHelper.Attach property. The attached property PasswordHelper.Password provides a bindable copy of the original password property of the PasswordBox control."

Published on -


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

1:  <PasswordBox w:PasswordHelper.Attach="True"   
2:       xmlns:w:PasswordHelper.Password="{Binding Text, ElementName=plain, Mode=TwoWay}"   
3:           Width="130"/>  

.NET , C# , WPF

0 comments :

Post a Comment

 

© 2011 GIS and Remote Sensing Tools, Tips and more .. ToS | Privacy Policy | Sitemap

About Me