source: extensions/PiwigoLib/PiwigoWpf/UI/PasswordHelper.cs @ 11911

Last change on this file since 11911 was 11911, checked in by bayral, 13 years ago

WPF inprovement

File size: 3.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows;
6using System.Windows.Controls;
7
8namespace Com.Piwigo.Wpf.UI
9{
10    public static class PasswordHelper
11    {
12        public static readonly DependencyProperty BoundPassword =
13          DependencyProperty.RegisterAttached("BoundPassword", typeof(string), typeof(PasswordHelper), new PropertyMetadata(string.Empty, OnBoundPasswordChanged));
14
15        public static readonly DependencyProperty BindPassword = DependencyProperty.RegisterAttached(
16            "BindPassword", typeof(bool), typeof(PasswordHelper), new PropertyMetadata(false, OnBindPasswordChanged));
17
18        private static readonly DependencyProperty UpdatingPassword =
19            DependencyProperty.RegisterAttached("UpdatingPassword", typeof(bool), typeof(PasswordHelper), new PropertyMetadata(false));
20
21        private static void OnBoundPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
22        {
23            PasswordBox box = d as PasswordBox;
24
25            // only handle this event when the property is attached to a PasswordBox
26            // and when the BindPassword attached property has been set to true
27            if (d == null || !GetBindPassword(d))
28            {
29                return;
30            }
31
32            // avoid recursive updating by ignoring the box's changed event
33            box.PasswordChanged -= HandlePasswordChanged;
34
35            string newPassword = (string)e.NewValue;
36
37            if (!GetUpdatingPassword(box))
38            {
39                box.Password = newPassword;
40            }
41
42            box.PasswordChanged += HandlePasswordChanged;
43        }
44
45        private static void OnBindPasswordChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)
46        {
47            // when the BindPassword attached property is set on a PasswordBox,
48            // start listening to its PasswordChanged event
49
50            PasswordBox box = dp as PasswordBox;
51
52            if (box == null)
53            {
54                return;
55            }
56
57            bool wasBound = (bool)(e.OldValue);
58            bool needToBind = (bool)(e.NewValue);
59
60            if (wasBound)
61            {
62                box.PasswordChanged -= HandlePasswordChanged;
63            }
64
65            if (needToBind)
66            {
67                box.PasswordChanged += HandlePasswordChanged;
68            }
69        }
70
71        private static void HandlePasswordChanged(object sender, RoutedEventArgs e)
72        {
73            PasswordBox box = sender as PasswordBox;
74
75            // set a flag to indicate that we're updating the password
76            SetUpdatingPassword(box, true);
77            // push the new password into the BoundPassword property
78            SetBoundPassword(box, box.Password);
79            SetUpdatingPassword(box, false);
80        }
81
82        public static void SetBindPassword(DependencyObject dp, bool value)
83        {
84            dp.SetValue(BindPassword, value);
85        }
86
87        public static bool GetBindPassword(DependencyObject dp)
88        {
89            return (bool)dp.GetValue(BindPassword);
90        }
91
92        public static string GetBoundPassword(DependencyObject dp)
93        {
94            return (string)dp.GetValue(BoundPassword);
95        }
96
97        public static void SetBoundPassword(DependencyObject dp, string value)
98        {
99            dp.SetValue(BoundPassword, value);
100        }
101
102        private static bool GetUpdatingPassword(DependencyObject dp)
103        {
104            return (bool)dp.GetValue(UpdatingPassword);
105        }
106
107        private static void SetUpdatingPassword(DependencyObject dp, bool value)
108        {
109            dp.SetValue(UpdatingPassword, value);
110        }
111    }
112}
Note: See TracBrowser for help on using the repository browser.