博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
silverlight-带水印的自定义TextBox控件(版本2)
阅读量:7174 次
发布时间:2019-06-29

本文共 3437 字,大约阅读时间需要 11 分钟。

之前那个版本《》不得不说是相当失败的,起码我是这样理解的。其实我一心想把这个给实现了,但是不得不承认自身技术上的缺陷。经过一番尝试和折腾,有了下面这个版本。

两个版本的区别:

其实是有本质的区别的。

之前那个版本只能设置“文本水印”,相当有局限性。现在的版本是可以自定义水印内容的。譬如,可以为button,rectangle,ellipsed... 

 

1.先新建一个“silverlight 模板化控件”,取名随意,此处为SuperText

2.修改继承的类Contol为TextBox

3.修改TextBox模板

控件SuperTextBox的模板,样式都是保存在一个叫“Generic.xaml”文件里面的。在第一次新建“silverlight 模板化控件”的时候,这个文件会自动产生。这里将TextBox的模板添加进入,并修改。

完整的

完整的Generic.xaml

 在上面的"Generic.xaml"文件的SuperTextBox模板中修改新增了一下内容

  • 新增一个UserControl,即水印内容。

取名随意,这里为“watermarkContent”,并且绑定Content属性为“WaterMark”

  •  增加两个状态

状态为了控制水印是否出现。其实本质是调节水印的透明度。

 

4.在托管代码中

  • 因为模板中绑定了一个新的属性WaterMark,所以需要在托管代码进行注册。并且用属性Watermark保存。
public object Watermark{    get { return base.GetValue(WatermarkProperty) as object; }    set { base.SetValue(WatermarkProperty, value); }}//向空间增加property public static readonly DependencyProperty WatermarkProperty =DependencyProperty.Register("Watermark", typeof(object), typeof(SuperTextBox), new PropertyMetadata(null));

 

 

  • 为了控制水印状态需要增加一个TextChanged事件。
public SuperTextBox(){    this.DefaultStyleKey = typeof(SuperTextBox);    TextChanged += new TextChangedEventHandler(SuperTextBox_TextChanged);}void SuperTextBox_TextChanged(object sender, TextChangedEventArgs e){    TextBox tb = (TextBox)sender;    if (tb.Text.Length > 0)    {        System.Windows.VisualStateManager.GoToState(tb, "Hidden", false);    }    else    {        System.Windows.VisualStateManager.GoToState(tb, "Shown", false);    } }

 

  • 完整托管代码
完整的SuperTextBox托管代码
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using System.Collections.ObjectModel;namespace TextBoxWaterMark{    public class SuperTextBox : TextBox    {        public object Watermark        {            get { return base.GetValue(WatermarkProperty) as object; }            set { base.SetValue(WatermarkProperty, value); }        }        //向空间增加property 瓶颈所在        public static readonly DependencyProperty WatermarkProperty =        DependencyProperty.Register("Watermark", typeof(object), typeof(SuperTextBox), new PropertyMetadata(null));        public SuperTextBox()        {            this.DefaultStyleKey = typeof(SuperTextBox);            TextChanged += new TextChangedEventHandler(SuperTextBox_TextChanged);        }        void SuperTextBox_TextChanged(object sender, TextChangedEventArgs e)        {            TextBox tb = (TextBox)sender;            if (tb.Text.Length > 0)            {                System.Windows.VisualStateManager.GoToState(tb, "Hidden", false);            }            else            {                System.Windows.VisualStateManager.GoToState(tb, "Shown", false);            }         }        public override void  OnApplyTemplate()        {            base.OnApplyTemplate();        }    }}

 

5.调用

需加上命名空间local,local指向的是SuperTextBox所在的命名空间。

6.效果

 

参考资料:《 》
《》
 
人生苦短,虚心求教

 

 

转载地址:http://umbzm.baihongyu.com/

你可能感兴趣的文章
mongo 手册阅读笔记
查看>>
viewport ——视区概念
查看>>
情商的管理
查看>>
页面跳转到Area区域连接
查看>>
拓扑规则翻译函数(转)
查看>>
[转载]SVN使用教程
查看>>
android 获取所有SD卡目录
查看>>
A trick in loading Fixture to test django application
查看>>
重建二叉树 (剑指offer第六题)
查看>>
爬虫基础 pyquery 详解
查看>>
QT creator+OpenCV2.4.2+MinGW 在windows下开发环境配置
查看>>
Allegro PCB Design GXL (legacy) 设置十字大光标
查看>>
数据结构--图的定义和存储结构
查看>>
[C#参考]委托机制
查看>>
linux常用命令
查看>>
自然杂志上的影评
查看>>
SQL Server 存储过程
查看>>
Appium自动化测试1 - 安装部署
查看>>
广州.NET微软技术俱乐部微信群各位技术大牛的blog
查看>>
《Redis设计与实现》之第九章:数据库
查看>>