silverlight WPF 水纹文本框

2019-09-29 17:22:12 浏览数 (1)

silverlight3取消了watertextbox控件,只有自己实现了个,实现了和textbox一样的无差异使用,只需要设置defaulttext就可以了

代码语言:javascript复制
  1 using System;
  2 using System.Windows;
  3 using System.Windows.Controls;
  4 using System.Windows.Media;
  5 
  6 namespace iLIS.Common.Controls
  7 {
  8     /// <summary>
  9     /// 自定义WatermarkedTextBox控件
 10     /// </summary>
 11     public class WatermarkedTextBox : TextBox
 12     {
 13         /// <summary>
 14         /// 水印文字
 15         /// </summary>
 16         public string DefaultText
 17         {
 18             get
 19             {
 20                 return (string)GetValue(DefaultTextProperty);
 21             }
 22             set
 23             {
 24                 SetValue(DefaultTextProperty, value);
 25             }
 26         }
 27         /// <summary>
 28         /// 文本框中的文字
 29         /// 文字为水印文字时返回空
 30         /// </summary>
 31         public string Text
 32         {
 33             get
 34             {
 35                 if (base.Text == DefaultText)
 36                 {
 37                     return string.Empty;
 38                 }
 39                 else
 40                 {
 41                     return base.Text;
 42                 }
 43             }
 44             set
 45             {
 46                 base.Text = value;
 47             }
 48         }
 49         /// <summary>
 50         /// 获取或设置一个用于描述前景色的画笔。
 51         /// 用于绘制控件的前景的画笔。默认值为 System.Windows.Media.Colors.Black。
 52         /// </summary>
 53         public new Brush Foreground
 54         {
 55             get
 56             {
 57                 return (Brush)GetValue(ForegroundProperty);
 58             }
 59             set
 60             {
 61                 SetValue(ForegroundProperty, value);
 62             }
 63         }
 64 
 65         // Using a DependencyProperty as the backing store for Foreground.  This enables animation, styling, binding, etc...
 66         public static new readonly DependencyProperty ForegroundProperty =
 67             DependencyProperty.Register("Foreground", typeof(Brush), typeof(WatermarkedTextBox), new PropertyMetadata((o, e) =>
 68             {
 69                 SolidColorBrush brush = e.NewValue as SolidColorBrush;
 70                 if (brush != null && brush.Color != Colors.Gray)
 71                 {
 72                     (o as WatermarkedTextBox).oldBrush = brush;
 73                 }
 74             }));
 75 
 76         private Brush oldBrush = new SolidColorBrush(Colors.Black);
 77         /// <summary>
 78         /// 默认文本
 79         /// </summary>
 80         public static readonly DependencyProperty DefaultTextProperty =
 81             DependencyProperty.Register("DefaultText", typeof(string), typeof(WatermarkedTextBox), new PropertyMetadata(""));
 82         public event TextChangedEventHandler WatermarkedTextChanged;
 83         /// <summary>
 84         /// 初始化 System.Windows.Controls.TextBox 类的新实例。
 85         /// </summary>
 86         public WatermarkedTextBox()
 87         {
 88             base.TextChanged  = new TextChangedEventHandler(OnWatermarkedTextBox_TextChanged);
 89         }
 90 
 91         /// <summary>
 92         ///在派生类中重写后,每当应用程序代码或内部进程(如重新生成布局处理过程)调用 System.Windows.Controls.Control.ApplyTemplate(),都将调用此方法。
 93         ///简而言之,这意味着就在UI 元素在应用程序中显示前调用该方法。有关更多信息,请参见“备注”。
 94         /// </summary>
 95         public override void OnApplyTemplate()
 96         {
 97             this.Text = this.DefaultText;
 98             base.OnApplyTemplate();
 99             base.Foreground = new SolidColorBrush(Colors.Gray);
100         }
101         /// <summary>
102         ///在 System.Windows.UIElement.GotFocus 事件发生之前调用。
103         /// </summary>
104         /// <param name="e">事件的数据</param>
105         protected override void OnGotFocus(RoutedEventArgs e)
106         {
107             if (string.Equals(base.Text, this.DefaultText, StringComparison.OrdinalIgnoreCase))
108             {
109                 base.Text = string.Empty;
110             }
111             base.OnGotFocus(e);
112         }
113         /// <summary>
114         ///在 System.Windows.UIElement.LostFocus 事件发生之前调用。
115         /// </summary>
116         /// <param name="e">事件的数据</param>
117         protected override void OnLostFocus(RoutedEventArgs e)
118         {
119             if (string.IsNullOrEmpty(base.Text))
120             {
121                 base.Text = this.DefaultText;
122                 base.Foreground = new SolidColorBrush(Colors.Gray);
123             }
124             base.OnLostFocus(e);
125         }
126         /// <summary>
127         /// 文本改变时调用
128         /// </summary>
129         /// <param name="sender"></param>
130         /// <param name="e"></param>
131         private void OnWatermarkedTextBox_TextChanged(object sender, TextChangedEventArgs e)
132         {
133             if (!string.Equals(base.Text, this.DefaultText, StringComparison.OrdinalIgnoreCase))
134             {
135                 base.Foreground = oldBrush;
136                 if (this.WatermarkedTextChanged != null)
137                 {
138                     this.WatermarkedTextChanged(this, e);
139                 }
140             }
141         }
142 
143     }
144 }

0 人点赞