实现给windows phone 水印TextBox
来源:互联网 发布:纯粮固态发酵白酒知乎 编辑:程序博客网 时间:2024/06/05 16:09
原理:在失去焦点和获取焦点的时候,判断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;
}
}
}
}
- 实现给windows phone 水印TextBox
- asp.net 实现水印textbox
- SilverLight TextBox水印效果实现
- windows phone中textbox只能输入数字
- 【小企】WPF实现TextBox水印效果
- WinForm控件美化扩展系列之给TextBox加水印
- windows控件实现水印
- 实现给图片加水印功能(jmagick实现,适用于windows系统)
- Windows Phone 7 扩展TextBox控件为数字输入文本框
- Windows Phone 7中带题头的TextBox和PasswordBox
- 一天一天学 windows phone 控件 之 TextBox + PasswordBox (十六)
- 使用javascript和css来实现textbox水印效果
- 实现Winform的TextBox自带水印提示信息
- C#中TextBox水印提示的实现过程
- WinForm实现为TextBox设置水印文字功能
- Winform 水印TextBox
- WPF 水印 TextBox MaskedTextBox
- windows phone 给系统控件颜色
- 关于spring的注解
- 心的开始
- Java 一些不注意的概念点
- Scrapy解决模拟登录问题
- 一个值得注意的const问题
- 实现给windows phone 水印TextBox
- php 表插入数据
- 一,入门基础—— 4,IOS模拟器的使用 截图
- C#学习有感(Lambda表达式)
- 黑马程序员.Android攻城狮.JAVA基础.1.2.基础语法
- 在MicroBlaze上跑FreeRTOS V8
- Spring中的事务传播行为
- Java语言基础特性—第一部分(上)
- SQL Server text field里面有换行符的时候copy到excel数据会散乱