前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >silverlight WPF 水纹文本框

silverlight WPF 水纹文本框

作者头像
用户6362579
发布2019-09-29 17:22:12
1.3K0
发布2019-09-29 17:22:12
举报
文章被收录于专栏:小神仙小神仙

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 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-02-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档