原文来自:wp教程网

原理:在失去焦点和获取焦点的时候,判断Text值是否为空或者是否与水印值相同,然后修改TextBox中的Text和Foreground。

代码如下:

/* ==============================================================================
2 * 类名称:WatermarkTextBox
3 * 类描述:
4 * 创建人:neoyee
5 * 创建时间:2014/2/25 17:24:11
6 * 修改人:
7 * 修改时间:
8 * 修改备注:
9 * @version 1.0
10 * ==============================================================================*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Windows.UI;

namespace WP8.Controls
{
public sealed class WatermarkTextBox : TextBox
{
private static readonly DependencyProperty WatermarkTextProperty =
DependencyProperty.Register("WatermarkText", typeof(string), typeof(WatermarkTextBox), new PropertyMetadata(string.Empty, new PropertyChangedCallback(WatermarkTextChanged)));

private static readonly DependencyProperty WatermarkForegroundProperty =
DependencyProperty.Register("WatermarkForeground", typeof(SolidColorBrush), typeof(WatermarkTextBox), new PropertyMetadata(new SolidColorBrush(Colors.Black)));

private static readonly DependencyProperty WatermarkBackgroundProperty =
DependencyProperty.Register("WatermarkBackground", typeof(SolidColorBrush), typeof(WatermarkTextBox), new PropertyMetadata(new SolidColorBrush(Colors.White)));

private static readonly DependencyProperty NormalForegroundProperty =
DependencyProperty.Register("NormalForeground", typeof(SolidColorBrush), typeof(WatermarkTextBox), new PropertyMetadata(new SolidColorBrush(Colors.Black), NormalForegroundPropertyChanged));

private static readonly DependencyProperty NormalBackgroundProperty =
DependencyProperty.Register("NormalBackground", typeof(SolidColorBrush), typeof(WatermarkTextBox), new PropertyMetadata(new SolidColorBrush(Colors.White)));

private static void NormalForegroundPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
var watermarkTextBox = obj as WatermarkTextBox;
if (watermarkTextBox != null)
watermarkTextBox.NormalForegroundChanged((SolidColorBrush)args.NewValue);
}

private void NormalForegroundChanged(SolidColorBrush value)
{
Foreground = value;
}

public SolidColorBrush NormalBackground
{
get { return (SolidColorBrush)GetValue(NormalBackgroundProperty); }
set { SetValue(NormalBackgroundProperty, value); }
}

public SolidColorBrush NormalForeground
{
get { return (SolidColorBrush)GetValue(NormalForegroundProperty); }
set { SetValue(NormalForegroundProperty, value); }
}
public SolidColorBrush WatermarkBackground
{
get { return (SolidColorBrush)GetValue(WatermarkBackgroundProperty); }
set { SetValue(WatermarkBackgroundProperty, value); }
}
public SolidColorBrush WatermarkForeground
{
get { return (SolidColorBrush)GetValue(WatermarkForegroundProperty); }
set { SetValue(WatermarkForegroundProperty, value); }
}

public string WatermarkText
{
get { return (string)GetValue(WatermarkTextProperty); }
set { SetValue(WatermarkTextProperty, value); }
}

public WatermarkTextBox()
{
this.LostFocus += WatermarkTextBox_LostFocus;
this.GotFocus += WatermarkTextBox_GotFocus;
this.TextChanged += WatermarkTextBox_TextChanged;
if (string.IsNullOrEmpty(this.Text))
{
this.Text = WatermarkText;
Foreground = WatermarkForeground;
}

}

void WatermarkTextBox_TextChanged(object sender, TextChangedEventArgs e)
{

if (Text == WatermarkText)
{
this.Text = WatermarkText;
Foreground = WatermarkForeground;
}
else if (Text != WatermarkText)
{
Foreground = NormalForeground;
}
}

private static void WatermarkTextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
((WatermarkTextBox)obj).WatermarkTextChanged(args.OldValue, args.NewValue);
}

private void WatermarkTextChanged(object OldValue, object NewValue)
{

}

void WatermarkTextBox_GotFocus(object sender, RoutedEventArgs e)
{
if (this.Text == WatermarkText && Foreground == WatermarkForeground)
{
this.Text = string.Empty;
Foreground = NormalForeground;
}
}

void WatermarkTextBox_LostFocus(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(this.Text) || Text == WatermarkText)
{
this.Text = WatermarkText;
Foreground = WatermarkForeground;
}
}
}
}

详细说明:http://wp.662p.com/thread-8105-1-1.html

windows phone 水印TextBox的更多相关文章

  1. Windows Phone 的 TextBox 的实现 PropertyChanged

    比如,View 的文本框 TextBox1 绑定了 ViewModel 的 Msg 属性, 当想把文本框输入的内容输入过程中实时更新到绑定的 Msg ,在Windows Phone 中是无法通过设置  ...

  2. Winform 水印TextBox

    方法一: public partial class WaterTextBox : TextBox { private readonly Label lblwaterText = new Label() ...

  3. WPF 水印TextBox WatermarkTextBox

    //https://blog.csdn.net/puchitomato/article/details/12248691 转自以上链接,自己添加了Enter响应事件.    public class ...

  4. winform的水印TextBox

    public partial class WaterTextBox : TextBox { private readonly Label lblwaterText = new Label(); pub ...

  5. WPF 自定义TextBox带水印控件,可设置圆角

    一.简单设置水印TextBox控件,废话不多说看代码: <TextBox TextWrapping="Wrap" Margin="10" Height=& ...

  6. WPF自定义控件与样式(3)-TextBox & RichTextBox & PasswordBox样式、水印、Label标签、功能扩展

    一.前言.预览 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要是对文本 ...

  7. 【转】WPF TextBox和PasswordBox加水印

    Textbox加水印 Textbox加水印,需要一个VisualBrush和触发器验证Text是否为空,在空的时候设置背景的Brush就可以实现水印效果. <TextBox Name=" ...

  8. 【转】WPF自定义控件与样式(3)-TextBox & RichTextBox & PasswordBox样式、水印、Label标签、功能扩展

    一.前言.预览 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要是对文本输入控件进行样式开发,及相关扩展功能开发,主要内容包括: 基本文 ...

  9. 解决Win10电脑右下角的“激活windows转到电脑设置”的水印的方法

    Win10正式版的用户反馈新系统在使用一段时候后,自己电脑桌面右下角就突然出现了“激活windows10转到设置以激活windows”的水印字样.这是怎么回事呢?下面,我就向大家分享win10电脑右下 ...

随机推荐

  1. SQL Server 查询分解

    标签:SQL SERVER/MSSQL SERVER/数据库/DBA/查询步骤 概述 查询步骤是很基础也挺重要的一部分,但是我还是在周围发现有些人虽然会语法,但是对于其中的步骤不是很清楚,这里就来分解 ...

  2. 有点担心Node.js的未来了

    原创文章转载请注明出处:@协思, http://zeeman.cnblogs.com 首先本文的目的不是引发语言之争,纯属个人的一些思绪记录. 因为工作原因,用Node.js做过几个项目,基本都是涉及 ...

  3. Android学习第三天-签名常用命令

    由于怕篇幅过长,所以把这个打包常用命令分开成两篇博文来进行讲解,下面我们直接进入主题吧. 8.keytool 这是我们JDK自带的密钥和证书管理工具 命令: -certreq 生成证书请求 -chan ...

  4. 控制Linux下 mono 服务的启动停止

    当Window下的服务部署到Linux的时候,我们一般用Mono.service 来启动停止.参数比较多,不太好用.于是有个这个Shell脚本. 用法:moa s1 start #启动         ...

  5. Atitit 深入理解耦合Coupling的原理与attilax总结

    Atitit 深入理解耦合Coupling的原理与attilax总结     耦合是指两个或两个以上的电路元件或电网络等的输入与输出之间存在紧密配合与相互影响,并通过相互作用从一侧向另一侧传输能量的现 ...

  6. Atitit 图像处理 深刻理解梯度原理计算.v1 qc8

    Atitit 图像处理 深刻理解梯度原理计算.v1 qc8 1.1. 图像处理  梯度计算  基本梯度 内部梯度 外部梯度 方向梯度1 2. 图像梯度就是图像边缘吗?2 1.1. 图像处理  梯度计算 ...

  7. WCF 安全性之 自定义用户名密码验证

    案例下载 http://download.csdn.net/detail/woxpp/4113172 客户端调用代码 通过代理类 代理生成 参见 http://www.cnblogs.com/woxp ...

  8. LeetCode OJ1:Reverse Words in a String

    问题描述: Given an input string, reverse the string word by word. For example,Given s = "the sky is ...

  9. Android开发-之数据的存储方式一

    在Android中,数据的存储分为两种方式: 1.直接以文件的形式存储在目录中 2.以json格式存储在数据库中 将数据以文件的存储又分为两种方式: 1.生成.txt文件 2.生成xml文件 那么今天 ...

  10. 使用 fixed role 授予权限

    今天下午,Leader 发mail给我,要求授予某个User对数据库只读的权限. Step1,在SQL Server中为该用户创建一个Login和User,在创建User时,建立Login 和 Use ...