微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SilverLight TextBox水印效果实现

在Silverlight2里面,提供了TextBox的水印WaterMark功能。但是之后的版本就把这个功能给删了。关于Silverlight2里面的水印功能可以参考这篇文章一步一步学Silverlight 2系列(2):基本控件》。之后想用水印唯有自己写一个了。

以下是我自己写的一个带水印的TextBox

1.新建类MyTextBox,继承TextBox

2.在MyTextBox类里面,增加一个属性WaterMarkText用来保存水印。

除了增加一个属性之外,还需要增加一些保存区别于正常状态的属性全局变量

private Brush _redColor = new SolidColorBrush(Colors.Red);
private double _halfOpacity = 0.5;

//正常状态
private Brush _userColor;
private double _userOpacity;

public string WaterMarkText { get; set; }

3.并且重写OnGotFocus()和OnLostFocus()两个事件。

在TextBox里面我们可以发现这两个事件是Override标记的,所以可以重载他们。

protected override void  OnGotFocus(RoutedEventArgs e)
{
    if (this.Text == WaterMarkText)
    {
        this.Text = "";
        this.Foreground = _userColor;
        this.Opacity = _userOpacity;
    }
    base.OnGotFocus(e);
}

protected override void OnLostFocus(RoutedEventArgs e)
{
    if (this.Text.Length < 1)
    {
        this.Text = WaterMarkText;
        this.Foreground = _redColor;
        this.Opacity = _halfOpacity;
    }
    base.OnLostFocus(e);
}

4.虽然这里已经完成大部分工作了,但是还有一个重要的地方。

类似于初始化,先验检测水印是否存在,而且设置水印。这个我将代码写在SizeChanged事件里面。为什么要写在这里可以参考另外一篇文章,关于控件的生命周期的《Silverlight 的控件生命周期 - 木野狐(Neil Chen)》。另外要将_userColor和_userOpacity初始化。

SizeChanged事件的代码如下:

public MyTextBox()
{
    SizeChanged += new SizeChangedEventHandler(MyTextBox_SizeChanged);
}

void MyTextBox_SizeChanged(object sender,SizeChangedEventArgs e)
{
    _userColor = this.Foreground;
    _userOpacity = this.Opacity;
    if (WaterMarkText != "")
    {
        this.Foreground = _redColor;
        this.Opacity = _halfOpacity;
        this.Text = WaterMarkText;
    }
}

5.源代码,至此工作完成。以下是完整代码

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace TextBoxWaterMark
{
    public class MyTextBox:TextBox
    {
        //水印状态
        private Brush _redColor = new SolidColorBrush(Colors.Red);
        private double _halfOpacity = 0.5;

        //正常状态
        private Brush _userColor;
        private double _userOpacity;

        public string WaterMarkText { get; set; }

        public MyTextBox()
        {
            SizeChanged += new SizeChangedEventHandler(MyTextBox_SizeChanged);
        }

        void MyTextBox_SizeChanged(object sender,SizeChangedEventArgs e)
        {
            _userColor = this.Foreground;
            _userOpacity = this.Opacity;
            if (WaterMarkText != "")
            {
                this.Foreground = _redColor;
                this.Opacity = _halfOpacity;
                this.Text = WaterMarkText;
            }
        }

        protected override void  OnGotFocus(RoutedEventArgs e)
        {
            if (this.Text == WaterMarkText)
            {
                this.Text = "";
                this.Foreground = _userColor;
                this.Opacity = _userOpacity;
            }
            base.OnGotFocus(e);
        }

        protected override void OnLostFocus(RoutedEventArgs e)
        {
            if (this.Text.Length < 1)
            {
                this.Text = WaterMarkText;
                this.Foreground = _redColor;
                this.Opacity = _halfOpacity;
            }
            base.OnLostFocus(e);
        }
    }
}

6.调用过程

<local:MyTextBox Foreground="Blue" WaterMarkText="请输入!" />

local是命名空间,是MyTextBox类所在的命名空间。本机是这样写的:xmlns:local="clr-namespace:TextBoxWaterMark"

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐