原文来自: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. 大白话讲解Promise(三)搞懂jquery中的Promise

    前两篇我们讲了ES6中的Promise以及Promise/A+规范,在Promise的知识体系中,jquery当然是必不可少的一环,所以本篇就来讲讲jquery中的Promise,也就是我们所知道的D ...

  2. Module Zero之用户管理

    返回<Module Zero学习目录> 用户实体 用户管理者 用户认证 用户实体 用户实体代表应用的一个用户,它派生自AbpUser类,如下所示: public class User : ...

  3. 为什么可以说Java语言是准动态语言?

    什么是动态语言? 动态语言,是指程序在运行时可以改变其结构:新的函数可以被引进,已有的函数可以被删除等在结构上的变化.比如JavaScript便是一个典型的动态语言. 除此之外如Ruby.Python ...

  4. JavaScript Json对象和Json对象字符串的关系 jsonObj<->JsonString

    JavaScript Json对象和Json对象字符串的关系 jsonObj<->JsonString 如下示例: 直接写的a1就是一个Json对象,a2 就是一个Json对象字符串; 通 ...

  5. react-native 简述

    1. RN 是什么 2. RN 与传统Hybrid框架相比的优势 3. RN的优势 4. RN的劣势 5. RN开发重点关注的问题 1 RN是什么 2 RN 与传统Hybrid框架相比的优势 传统的如 ...

  6. SQL Server 数据库子查询基本语法

    一.SQL子查询语句 1.单行子查询        select ename,deptno,sal        from emp        where deptno=(select deptno ...

  7. 【开源】OSharp框架解说系列(1):总体设计及系列导航

    系列文章导航 [开源]OSharp框架解说系列(1):总体设计 [开源]OSharp框架解说系列(2.1):EasyUI的后台界面搭建及极致重构 [开源]OSharp框架解说系列(2.2):EasyU ...

  8. Android接入百度自动更新SDK

    一:前言 公司的app,上传到百度应用市场,然后说必须要接入百度的自动更新sdk才能上架,于是从百度官网上去下载jar包,下载的时候必须要带上数据统计,如果使用自动的jar包,还需要带上广告联盟,坑爹 ...

  9. 搭建Spark的单机版集群

    一.创建用户 # useradd spark # passwd spark 二.下载软件 JDK,Scala,SBT,Maven 版本信息如下: JDK jdk-7u79-linux-x64.gz S ...

  10. 如何利用Oracle外部表导入文本文件的数据

    同事最近在忙数据一致性比对工作,需要对不同文本文件中的数据进行比对,有的文件较大,记录较多,如果用普通的文本编辑器打开的话,很显然,会很卡,甚至打不开. 基于此,可将该文本文件的数据导入到数据库中,在 ...