Mercurial > silverbladetech
comparison Chronosv2/source/Presentation/Controls/Interactivity/Behaviors/Watermark/PasswordBoxWatermarkBehavior.cs @ 10:443821e55f06
Initial cleaned up add from Codeplex files
author | stevenh7776 stevenhollidge@hotmail.com |
---|---|
date | Tue, 21 Feb 2012 17:25:44 +0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
9:904a9faadf8b | 10:443821e55f06 |
---|---|
1 using System; | |
2 using System.Windows; | |
3 using System.Windows.Controls; | |
4 using System.Windows.Controls.Primitives; | |
5 using Chronos.Presentation.Controls.Adorner; | |
6 using Chronos.Presentation.Controls.Events; | |
7 | |
8 namespace Chronos.Presentation.Controls.Interactivity.Behaviors | |
9 { | |
10 /// <summary> | |
11 /// http://blindmeis.wordpress.com/2010/07/16/wpf-watermark-textbox-behavior/ | |
12 /// </summary> | |
13 public sealed class PasswordBoxWatermarkBehavior | |
14 : System.Windows.Interactivity.Behavior<PasswordBox> | |
15 { | |
16 #region · Attached Properties · | |
17 | |
18 public static readonly DependencyProperty LabelProperty = | |
19 DependencyProperty.RegisterAttached("Label", typeof(string), typeof(PasswordBoxWatermarkBehavior)); | |
20 | |
21 public static readonly DependencyProperty LabelStyleProperty = | |
22 DependencyProperty.RegisterAttached("LabelStyle", typeof(Style), typeof(PasswordBoxWatermarkBehavior)); | |
23 | |
24 #endregion | |
25 | |
26 #region · Fields · | |
27 | |
28 private TextBlockAdorner adorner; | |
29 private WeakPropertyChangeNotifier notifier; | |
30 | |
31 #endregion | |
32 | |
33 #region · Properties · | |
34 | |
35 public string Label | |
36 { | |
37 get { return (string)base.GetValue(LabelProperty); } | |
38 set { base.SetValue(LabelProperty, value); } | |
39 } | |
40 | |
41 public Style LabelStyle | |
42 { | |
43 get { return (Style)base.GetValue(LabelStyleProperty); } | |
44 set { base.SetValue(LabelStyleProperty, value); } | |
45 } | |
46 | |
47 #endregion | |
48 | |
49 #region · Overriden Methods · | |
50 | |
51 protected override void OnAttached() | |
52 { | |
53 base.OnAttached(); | |
54 | |
55 this.AssociatedObject.Loaded += this.AssociatedObjectLoaded; | |
56 this.AssociatedObject.PasswordChanged += this.AssociatedObjectPasswordChanged; | |
57 } | |
58 | |
59 protected override void OnDetaching() | |
60 { | |
61 base.OnDetaching(); | |
62 this.AssociatedObject.Loaded -= this.AssociatedObjectLoaded; | |
63 this.AssociatedObject.PasswordChanged -= this.AssociatedObjectPasswordChanged; | |
64 | |
65 this.notifier = null; | |
66 } | |
67 | |
68 #endregion | |
69 | |
70 #region · Private Methods · | |
71 | |
72 private void AssociatedObjectPasswordChanged(object sender, System.Windows.RoutedEventArgs e) | |
73 { | |
74 this.UpdateAdorner(); | |
75 } | |
76 | |
77 private void AssociatedObjectLoaded(object sender, System.Windows.RoutedEventArgs e) | |
78 { | |
79 this.adorner = new TextBlockAdorner(this.AssociatedObject, this.Label, this.LabelStyle); | |
80 | |
81 this.UpdateAdorner(); | |
82 | |
83 //AddValueChanged for IsFocused in a weak manner | |
84 this.notifier = new WeakPropertyChangeNotifier(this.AssociatedObject, UIElement.IsFocusedProperty); | |
85 this.notifier.ValueChanged += new EventHandler(this.UpdateAdorner); | |
86 } | |
87 | |
88 private void UpdateAdorner(object sender, EventArgs e) | |
89 { | |
90 this.UpdateAdorner(); | |
91 } | |
92 | |
93 private void UpdateAdorner() | |
94 { | |
95 if (!String.IsNullOrEmpty(this.AssociatedObject.Password) || this.AssociatedObject.IsFocused) | |
96 { | |
97 // Hide the Watermark Label if the adorner layer is visible | |
98 this.AssociatedObject.TryRemoveAdorners<TextBlockAdorner>(); | |
99 } | |
100 else | |
101 { | |
102 // Show the Watermark Label if the adorner layer is visible | |
103 this.AssociatedObject.TryAddAdorner<TextBlockAdorner>(adorner); | |
104 } | |
105 } | |
106 | |
107 #endregion | |
108 } | |
109 } |