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电脑右下 ...
随机推荐
- 纸上谈兵: 图 (graph)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 图(graph)是一种比较松散的数据结构.它有一些节点(vertice),在某些节 ...
- 实现 Math.Asin 迈克劳林(泰勒)展开式,结果比Math.Asin 慢一倍
项目中需要快速求解Asin(x) 的近似值,原以为用泰勒展开式会快一些,结果比原生的慢一倍. Math.ASin Time Elapsed: 9ms Gen 0: ...
- Android开发学习之路-RecyclerView使用初探
在进行一些MaterialDesign规范开发的时候,比如之前说到的CoordinateLayout实现的向上折叠效果的时候,如果依然使用ListView,那么这种效果是做不出来的,因为ListVie ...
- FASTJSON
package com.hanqi.test; import java.util.ArrayList;import java.util.Date;import java.util.List; impo ...
- SpringMVC常用的注解
1. @Controller @Controller用于标识控制层主键,负责注册一个bean到spring上下文,bean的ID默认为首字母小写的类名称,用户也可以自定义. 例如: 方式一: @Co ...
- WPF入门教程系列二十二——DataGrid示例(二)
DataGrid示例的后台代码 1) 通过Entity Framework 6.1 从数据库(本地数据库(local)/Test中的S_City表中读取城市信息数据,从S_ Province表中读取 ...
- UIViewController 生命周期
创建: 1. alloc 创建对象,分配空间 2.init (initWithNibName) 初始化对象,初始化数据 3.loadView 从nib载入视图 ,通常这一步不需要去干涉.除非你没有使用 ...
- WPF绘制折线
WPF后台绘制折线,填充到一个GRID下 private void btnPreview_Click(object sender, RoutedEventArgs e) { GridImg.Child ...
- HttpUrlConnection发送url请求(后台springmvc)
1.HttpURLConnection发送url请求 public class JavaRequest { private static final String BASE_URL = "h ...
- 配置 linux-bridge mechanism driver - 每天5分钟玩转 OpenStack(77)
本节开始我们将学习 Linux Bridge 如何实现 Neutron 的各种功能.首先需要配置 linux-bridge mechanism driver. Neutorn ML2 plugin 默 ...