Mercurial > silverbladetech
comparison Chronosv2/source/Presentation/Controls/Interactivity/Behaviors/Watermark/TextBoxWatermarkBehavior.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 Chronos.Presentation.Controls.Adorner; | |
5 using Chronos.Presentation.Controls.Events; | |
6 | |
7 namespace Chronos.Presentation.Controls.Interactivity.Behaviors | |
8 { | |
9 /// <summary> | |
10 /// http://blindmeis.wordpress.com/2010/07/16/wpf-watermark-textbox-behavior/ | |
11 /// </summary> | |
12 public sealed class TextBoxWatermarkBehavior | |
13 : System.Windows.Interactivity.Behavior<TextBox> | |
14 { | |
15 #region · Attached Properties · | |
16 | |
17 public static readonly DependencyProperty LabelProperty = | |
18 DependencyProperty.RegisterAttached("Label", typeof(string), typeof(TextBoxWatermarkBehavior)); | |
19 | |
20 public static readonly DependencyProperty LabelStyleProperty = | |
21 DependencyProperty.RegisterAttached("LabelStyle", typeof(Style), typeof(TextBoxWatermarkBehavior)); | |
22 | |
23 #endregion | |
24 | |
25 #region · Fields · | |
26 | |
27 private TextBlockAdorner adorner; | |
28 private WeakPropertyChangeNotifier notifier; | |
29 | |
30 #endregion | |
31 | |
32 #region · Properties · | |
33 | |
34 public string Label | |
35 { | |
36 get { return (string)base.GetValue(LabelProperty); } | |
37 set { base.SetValue(LabelProperty, value); } | |
38 } | |
39 | |
40 public Style LabelStyle | |
41 { | |
42 get { return (Style)base.GetValue(LabelStyleProperty); } | |
43 set { base.SetValue(LabelStyleProperty, value); } | |
44 } | |
45 | |
46 #endregion | |
47 | |
48 #region · Overriden Methods · | |
49 | |
50 protected override void OnAttached() | |
51 { | |
52 base.OnAttached(); | |
53 | |
54 this.AssociatedObject.Loaded += this.AssociatedObjectLoaded; | |
55 this.AssociatedObject.TextChanged += this.AssociatedObjectTextChanged; | |
56 } | |
57 | |
58 protected override void OnDetaching() | |
59 { | |
60 base.OnDetaching(); | |
61 | |
62 this.AssociatedObject.Loaded -= this.AssociatedObjectLoaded; | |
63 this.AssociatedObject.TextChanged -= this.AssociatedObjectTextChanged; | |
64 | |
65 this.notifier = null; | |
66 } | |
67 | |
68 #endregion | |
69 | |
70 #region · Private Methods · | |
71 | |
72 private void AssociatedObjectTextChanged(object sender, TextChangedEventArgs 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.Text) || 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 } |