• 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.源代码,至此工作完成。以下是完整代码:

    带水印的TextBox
    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"

    效果图如下:

    未获取焦点:

    获取焦点并输入

    好记性不如烂笔头
  • 相关阅读:
    给定一个字符串,打印输出有重复的字符和重复的次数,并且按照重复的次数升序输出
    Failed to bind NettyServer on /10.254.4.57:20880, cause: Failed to bind to: /0.0.0.0:20880 配置dubbo遇到的问题
    Feign远程调用,调用方法有返回值,没有返回原方法,Canal监听数据库发生的异常:end of stream when reading header,异常中还有“你的主机中的软件中止了一个已建立的连接”等信息
    idea格式化代码把方法上的文字注释换行的问题
    使用Docker报的错误 docker WARNING: IPv4 forwarding is disabled. Networking will not work
    11月新的开始
    二叉树学习总结
    leetcode刷题日志(200913)637.二叉树的层平均值
    leetcode刷题日志(200909)1512.好数对的数目
    leetcode刷题日志(200908)1480.一维数组动态和
  • 原文地址:https://www.cnblogs.com/rond/p/2650648.html
Copyright © 2020-2023  润新知