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

 using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media; namespace iLIS.Common.Controls
{
/// <summary>
/// 自定义WatermarkedTextBox控件
/// </summary>
public class WatermarkedTextBox : TextBox
{
/// <summary>
/// 水印文字
/// </summary>
public string DefaultText
{
get
{
return (string)GetValue(DefaultTextProperty);
}
set
{
SetValue(DefaultTextProperty, value);
}
}
/// <summary>
/// 文本框中的文字
/// 文字为水印文字时返回空
/// </summary>
public string Text
{
get
{
if (base.Text == DefaultText)
{
return string.Empty;
}
else
{
return base.Text;
}
}
set
{
base.Text = value;
}
}
/// <summary>
/// 获取或设置一个用于描述前景色的画笔。
/// 用于绘制控件的前景的画笔。默认值为 System.Windows.Media.Colors.Black。
/// </summary>
public new Brush Foreground
{
get
{
return (Brush)GetValue(ForegroundProperty);
}
set
{
SetValue(ForegroundProperty, value);
}
} // Using a DependencyProperty as the backing store for Foreground. This enables animation, styling, binding, etc...
public static new readonly DependencyProperty ForegroundProperty =
DependencyProperty.Register("Foreground", typeof(Brush), typeof(WatermarkedTextBox), new PropertyMetadata((o, e) =>
{
SolidColorBrush brush = e.NewValue as SolidColorBrush;
if (brush != null && brush.Color != Colors.Gray)
{
(o as WatermarkedTextBox).oldBrush = brush;
}
})); private Brush oldBrush = new SolidColorBrush(Colors.Black);
/// <summary>
/// 默认文本
/// </summary>
public static readonly DependencyProperty DefaultTextProperty =
DependencyProperty.Register("DefaultText", typeof(string), typeof(WatermarkedTextBox), new PropertyMetadata(""));
public event TextChangedEventHandler WatermarkedTextChanged;
/// <summary>
/// 初始化 System.Windows.Controls.TextBox 类的新实例。
/// </summary>
public WatermarkedTextBox()
{
base.TextChanged += new TextChangedEventHandler(OnWatermarkedTextBox_TextChanged);
} /// <summary>
///在派生类中重写后,每当应用程序代码或内部进程(如重新生成布局处理过程)调用 System.Windows.Controls.Control.ApplyTemplate(),都将调用此方法。
///简而言之,这意味着就在UI 元素在应用程序中显示前调用该方法。有关更多信息,请参见“备注”。
/// </summary>
public override void OnApplyTemplate()
{
this.Text = this.DefaultText;
base.OnApplyTemplate();
base.Foreground = new SolidColorBrush(Colors.Gray);
}
/// <summary>
///在 System.Windows.UIElement.GotFocus 事件发生之前调用。
/// </summary>
/// <param name="e">事件的数据</param>
protected override void OnGotFocus(RoutedEventArgs e)
{
if (string.Equals(base.Text, this.DefaultText, StringComparison.OrdinalIgnoreCase))
{
base.Text = string.Empty;
}
base.OnGotFocus(e);
}
/// <summary>
///在 System.Windows.UIElement.LostFocus 事件发生之前调用。
/// </summary>
/// <param name="e">事件的数据</param>
protected override void OnLostFocus(RoutedEventArgs e)
{
if (string.IsNullOrEmpty(base.Text))
{
base.Text = this.DefaultText;
base.Foreground = new SolidColorBrush(Colors.Gray);
}
base.OnLostFocus(e);
}
/// <summary>
/// 文本改变时调用
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnWatermarkedTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (!string.Equals(base.Text, this.DefaultText, StringComparison.OrdinalIgnoreCase))
{
base.Foreground = oldBrush;
if (this.WatermarkedTextChanged != null)
{
this.WatermarkedTextChanged(this, e);
}
}
} }
}

silverlight WPF 水纹文本框的更多相关文章

  1. WPF 自定义文本框输入法 IME 跟随光标

    本文告诉大家在 WPF 写一个自定义的文本框,如何实现让输入法跟随光标 本文非小白向,本文适合想开发自定义的文本框,从底层开始开发的文本库的伙伴.在开始之前,期望了解了文本库开发的基础知识 本文实现的 ...

  2. WPF 之 文本框及密码框添加水印效果

    1.文本框添加水印效果 文本框水印相对简单,不需要重写模板,仅仅需要一个 VisualBrush 和触发器验证一下Text是否为空即可. <TextBox Name="txtSerac ...

  3. wpf中文本框只能输入整数

    private void txtBarCodeNum_KeyUp(object sender, KeyEventArgs e) { TxtInt(sender as TextBox); } priva ...

  4. [WPF]带下拉列表的文本框

    控件我已经弄好了,代码比较多,所以没办法全面介绍. 一开始我是直接继承Selector类来实现,做是做出来了,不过发现性能不太好.于是,我就想着自己来实现.毕竟我是做给自己用的,也不考虑过多的东西,也 ...

  5. Silverlight中文本框添加回车事件后,换行无法清除的解决方法

    在开发Silverlight的项目中,为了更好的用户体验,我们常要给一些控件添加一些快捷键.然而,在Silverlight中当用户回车提交后,光标停留在文本框的第二行怎么也清除不掉,经过一段时间研究, ...

  6. WPF 文本框添加水印效果

    有的时候我们需要为我们的WPF文本框TextBox控件添加一个显示水印的效果来增强用户体验,比如登陆的时候提示输入用户名,输入密码等情形.如下图所示: 这个时候我们除了可以修改TextBox控件的控件 ...

  7. WPF文本框密码框添加水印效果

    WPF文本框密码框添加水印效果 来源: 阅读:559 时间:2014-12-31 分享: 0 按照惯例,先看下效果 文本框水印 文本框水印相对简单,不需要重写模板,仅仅需要一个VisualBrush ...

  8. WPF编程:textbox控件文本框数据显示最后一行

    WPF编程:textbox控件文本框数据显示最后一行 TextBox控件在接收大量数据的时候,滚动条一般在最上方,如何使滚动条随着数据的接收而向下滚动呢?比如有一个TextBox'控件txbRecvD ...

  9. WPF里面制作圆角文本框

    转自:http://www.cnblogs.com/mengxin523/archive/2010/04/04/1704448.html 本以为WPF里面的XAML会很强大,可以设置很多属性,比如文本 ...

随机推荐

  1. iOSiOS开发之退出功能(易错)

    如果,我们有两个控制器,第一个控制器是MainController,它是与Main.storyboard相关联的.第二个控制器是myController.假设myController中有一个退出按钮, ...

  2. 将node.js程序作为服务,并在windows下开机自动启动(使用forever)

    手上项目中有一块服务是用node.js实现的,运行环境是windows server 2008 R2,刚开始着手实现这块功能的时候时间很紧迫,随便写了个console程序就部署上去了--启动方式就是在 ...

  3. 网关、DNS、路由器区别

    1.首先得区分一下网关和路由器的区别: 网关是一个IP地址.是一个网络连接到另一个网络的"关口". 路由器是一个物理设备.一般局域网的网关就是路由器的IP地址. 2. 网关.DNS ...

  4. PHP和js实时倒计时

    <?php //这是t.php页面 header('content-type:text/html;charset=utf-8'); date_default_timezone_set('PRC' ...

  5. HTTP [TCP Retransmission] Continuation or non-HTTP traffic[Packet size limited during capture]

    http://www.xianren.org/blog/net/wireshark-q.html  抓到的包数据中常见的错误..待细看,先记下. tcpdump 抓包后发现,出现大量标题字样显示,不利 ...

  6. PRINCE2学习

    今天对PRINCE2中提及的7大主题进行学习,主要的内容是通过概述和PMBOK之间的对比做一些总结,每个主题所包含的过程和方法并没有太多涉及,没有对整个体系有全面深入的学习,有些断章取义的地方也请博友 ...

  7. linux系统使用python监测网络接口获取网络的输入输出

    #!/usr/bin/env Pythonimport timeimport sys if len(sys.argv) > 1: INTERFACE = sys.argv[1]else: INT ...

  8. 安装Postgresql

    p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; line-height: 150%; fon ...

  9. 关于压缩jar包时提示*.*没有这个文件或目录的问题以及解决办法:

    关于压缩jar包时提示.没有这个文件或目录的问题以及解决办法: 问题描述: 我在打包jar时,CMD中进入到包的上一层目录. 在命令提示符中输入 提示如下: 从提示中可知没有找到我们想要打包的clas ...

  10. 老李推荐:第5章3节《MonkeyRunner源码剖析》Monkey原理分析-启动运行: 启动脚本

    老李推荐:第5章3节<MonkeyRunner源码剖析>Monkey原理分析-启动运行: 启动脚本   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性 ...