windows phone 水印TextBox
原文来自: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的更多相关文章
- Windows Phone 的 TextBox 的实现 PropertyChanged
比如,View 的文本框 TextBox1 绑定了 ViewModel 的 Msg 属性, 当想把文本框输入的内容输入过程中实时更新到绑定的 Msg ,在Windows Phone 中是无法通过设置 ...
- Winform 水印TextBox
方法一: public partial class WaterTextBox : TextBox { private readonly Label lblwaterText = new Label() ...
- WPF 水印TextBox WatermarkTextBox
//https://blog.csdn.net/puchitomato/article/details/12248691 转自以上链接,自己添加了Enter响应事件. public class ...
- winform的水印TextBox
public partial class WaterTextBox : TextBox { private readonly Label lblwaterText = new Label(); pub ...
- WPF 自定义TextBox带水印控件,可设置圆角
一.简单设置水印TextBox控件,废话不多说看代码: <TextBox TextWrapping="Wrap" Margin="10" Height=& ...
- WPF自定义控件与样式(3)-TextBox & RichTextBox & PasswordBox样式、水印、Label标签、功能扩展
一.前言.预览 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要是对文本 ...
- 【转】WPF TextBox和PasswordBox加水印
Textbox加水印 Textbox加水印,需要一个VisualBrush和触发器验证Text是否为空,在空的时候设置背景的Brush就可以实现水印效果. <TextBox Name=" ...
- 【转】WPF自定义控件与样式(3)-TextBox & RichTextBox & PasswordBox样式、水印、Label标签、功能扩展
一.前言.预览 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要是对文本输入控件进行样式开发,及相关扩展功能开发,主要内容包括: 基本文 ...
- 解决Win10电脑右下角的“激活windows转到电脑设置”的水印的方法
Win10正式版的用户反馈新系统在使用一段时候后,自己电脑桌面右下角就突然出现了“激活windows10转到设置以激活windows”的水印字样.这是怎么回事呢?下面,我就向大家分享win10电脑右下 ...
随机推荐
- Entity Framework Code First数据库连接
1. 安装Entity Framework 使用NuGet安装Entity Framework程序包:工具->库程序包管理器->程序包管理器控制台,执行以下语句: PM> Insta ...
- Docker+nginx+tomcat7配置简单的负载均衡
本文为原创,原始地址为:http://www.cnblogs.com/fengzheng/p/4995513.html 本文介绍在Docker上配置简单的负载均衡,宿主机为Ubuntu 14.04.2 ...
- Step by step Install a Local Report Server and Remote Report Server Database
原创地址:http://www.cnblogs.com/jfzhu/p/4012097.html 转载请注明出处 前面的文章<Step by step SQL Server 2012的安装 &g ...
- EF:Fluent API 把一对多映射为一对一
假设有两张表:A表和B表.A表与B表在数据库中的关系是一对多,但我们需要在EF中映射为一对一. 首先在A实体类和B实体类中互相为对方增加一个实体类的属性: public A { public B B ...
- Clang Format
1,最近项目代码要求规范化,在网上找了个Xcode插件:Clang Format ,下载地址:https://github.com/travisjeffery/ClangFormat-Xcode 2, ...
- js事件(event)的运行原理
昨天写click事件时候突然脑袋抽筋想了想浏览器是怎么执行click事件的,为什么我们可以用e或者window.event这个对象获取一些事件的属性呐?以下是我的理解.如果您有更好的理解,欢迎评论!! ...
- 让IE系列支持HTML5的html5shiv.js和respond.min.js
HTML5越来越成为主流,被广大搜索引擎所使用,但IE对HTML5的支持却常被人唾弃. 解决方案有两种: 1.为网站创建多套模板,通过程序对User-Agent的判断给不同的浏览器用户显示不同的页面, ...
- Security2:Create User
User 用于访问DB Users based on logins in master (This is the most common type of user.) User based on a ...
- 上传伪技术~很多人都以为判断了后缀,判断了ContentType,判断了头文件就真的安全了。是吗?
今天群里有人聊图片上传,简单说下自己的经验(大牛勿喷) 0.如果你的方法里面是有指定路径的,记得一定要过滤../,比如你把 aa文件夹设置了权限,一些类似于exe,asp,php之类的文件不能执行,那 ...
- 【开源】OSharp3.0框架解说系列(6.2):操作日志与数据日志
OSharp是什么? OSharp是个快速开发框架,但不是一个大而全的包罗万象的框架,严格的说,OSharp中什么都没有实现.与其他大而全的框架最大的不同点,就是OSharp只做抽象封装,不做实现.依 ...