设计出来的样子是这个样子的。

注:这是个封装在DLL中的控件,但不影响理解

一、样式字典 - 仅仅定义了基本的骨骼结构

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><!--该字典中除非特殊声明,否则资源一律不能删除,会照成异常,因动态改变资源所以请务必不要随便改变动态资源为静态--><!--TextBox视图--><VisualBrush x:Key="TextBoxVisual"><VisualBrush.Visual><TextBlock></TextBlock></VisualBrush.Visual></VisualBrush><!--TextBoxMasked样式--><Style TargetType="TextBox" x:Key="TextBoxStyle"><Style.Triggers><Trigger Property="Text" Value="{x:Null}"><Setter Property="Background" Value="{DynamicResource TextBoxVisual}"></Setter></Trigger><Trigger Property="Text" Value=""><Setter Property="Background" Value="{DynamicResource TextBoxVisual}"></Setter></Trigger><Trigger Property="IsFocused" Value="True"><Setter Property="Background" Value="{x:Null}"></Setter></Trigger></Style.Triggers></Style>
</ResourceDictionary>

二、控件的封装:定义了几个依赖属性用于界面设置

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;namespace DialogEx.Controls
{/// <summary>/// 水印TextBox/// </summary>public class TextBoxMasked : TextBox{#region 依赖属性public static readonly DependencyProperty MaskTextProperty = DependencyProperty.Register("MaskText", typeof(string), typeof(TextBoxMasked),new PropertyMetadata(string.Empty));public static readonly DependencyProperty MaskTextFontStyleProperty = DependencyProperty.Register("MaskFontStyle", typeof(FontStyle), typeof(TextBoxMasked),new PropertyMetadata(FontStyles.Italic));public static readonly DependencyProperty MaskTextVerticalContentProperty = DependencyProperty.Register("MaskVerticalContent", typeof(AlignmentY), typeof(TextBoxMasked), new PropertyMetadata(AlignmentY.Center));public static readonly DependencyProperty MaskTextHorizontalAlignmentContentProperty = DependencyProperty.Register("MaskHorizontalContent", typeof(AlignmentX), typeof(TextBoxMasked), new PropertyMetadata(AlignmentX.Left));public static readonly DependencyProperty MaskTextFontSizeProperty = DependencyProperty.Register("MaskFontSize", typeof(Double), typeof(TextBoxMasked), new PropertyMetadata((Double)12));public static readonly DependencyProperty MaskTextLineHeightProperty = DependencyProperty.Register("MaskLineHeight", typeof(Double), typeof(TextBoxMasked), new PropertyMetadata((Double)12));#endregion#region 公有字段/// <summary>/// 内容/// </summary>public string MaskedText{get { return ((string)GetValue(MaskTextProperty)).Replace("\\r\\n", "\r\n"); }set { SetValue(MaskTextProperty, value); }}/// <summary>/// 字体样式/// </summary>public FontStyle MaskFontStyle{get { return (FontStyle)GetValue(MaskTextFontStyleProperty); }set { SetValue(MaskTextFontStyleProperty, value); }}/// <summary>/// 字体大小/// </summary>public double MaskFontSize{get { return (double)GetValue(MaskTextFontSizeProperty); }set { SetValue(MaskTextFontSizeProperty, value); }}/// <summary>/// 字体大小/// </summary>public double MaskLineHeight{get { return (double)GetValue(MaskTextLineHeightProperty); }set { SetValue(MaskTextLineHeightProperty, value); }}/// <summary>/// 垂直位置/// </summary>public AlignmentY MaskVerticalContent{get { return (AlignmentY)GetValue(MaskTextVerticalContentProperty); }set { SetValue(MaskTextVerticalContentProperty, value); }}/// <summary>/// 水平位置/// </summary>public AlignmentX MaskHorizontalContent{get { return (AlignmentX)GetValue(MaskTextHorizontalAlignmentContentProperty); }set { SetValue(MaskTextHorizontalAlignmentContentProperty, value); }}#endregion#region 私有字段#endregionpublic TextBoxMasked(){Loaded += (sender, args) =>{//资源视图VisualBrush _MaskVisual = new VisualBrush(){TileMode = TileMode.None,Opacity = 0.3,Stretch = Stretch.None,AlignmentX = this.MaskHorizontalContent,AlignmentY = this.MaskVerticalContent};_MaskVisual.Visual = new TextBlock(){FontStyle = this.MaskFontStyle,FontSize = this.MaskFontSize,Text = this.MaskedText,LineHeight = this.MaskLineHeight,};资源引用//this.Resources.Add("HelperBrush", _MaskVisual);//#region 触发器Setter//Setter NullSetter = new Setter()//{//    Property = BackgroundProperty,//    Value = this.Resources["HelperBrush"]//};//Setter EmptySetter = new Setter()//{//    Property = BackgroundProperty,//    Value = this.Resources["HelperBrush"]//};//Setter FocusedSetter = new Setter()//{//    Property = BackgroundProperty,//    Value = null//};//#endregion//#region 触发器//Trigger NullTrigger = new Trigger()//{//    Property = TextProperty,//    Value = null//};//NullTrigger.Setters.Add(NullSetter);//Trigger EmptyTrigger = new Trigger()//{//    Property = TextProperty,//    Value = ""//};//EmptyTrigger.Setters.Add(EmptySetter);//Trigger FocusedTrigger = new Trigger()//{//    Property = IsFocusedProperty,//    Value = true//};//FocusedTrigger.Setters.Add(FocusedSetter);//#endregion//#region 样式//Style NewStyle = new Style();//NewStyle.TargetType = typeof(TextBox);//NewStyle.Triggers.Add(NullTrigger);//NewStyle.Triggers.Add(EmptyTrigger);//NewStyle.Triggers.Add(FocusedTrigger);//this.Style = NewStyle;//#endregionthis.Resources.Source = new Uri("DialogEx;Component/Controls/TextBoxMasked.xaml", UriKind.RelativeOrAbsolute);this.Resources["TextBoxVisual"] = _MaskVisual;this.Style = this.Resources["TextBoxStyle"] as Style;};}}
}

三、使用方法 - 引用文本框的命名空间。

xmlns:MyControls="clr-namespace:DialogEx.Controls;assembly=DialogEx"<MyControls:TextBoxMasked Height="28" Width="350" VerticalContentAlignment="Center" BorderBrush="#197CD6" BorderThickness="1" Text="{Binding SearchText}" Foreground="Black" FontSize="12" MaskedText="请输入搜索关键词" MaskFontStyle="Italic" MaskVerticalContent="Center"/>

WPF 水印 TextBox MaskedTextBox相关推荐

  1. WPF实现TextBox水印效果

    原文:WPF实现TextBox水印效果 在日常项目中,一个TextBox需要输入用户名,我们通常的做法是先用一个TextBlock来说明,例如下面的截图: 今天将使用另外一种方式来展示,使用水印的方式 ...

  2. C#WPF实现TextBox控件水印效果的两种实现方法

    C#WPF实现TextBox控件水印效果的两种实现方法 在WPF实际项目中往往需要在TextBox中加入水印,来告诉使用者输入TextBox中的内容,如下图片所示: 下面介绍两种方式,来添加上图中的水 ...

  3. Wpf解决TextBox文件拖入问题、拖放问题

    在WPF中,当我们尝试向TextBox中拖放文件,从而获取其路径时,往往无法成功(拖放文字可以成功).造成这种原因关键是WPF的TextBox对拖放事件处理机制的不同, 解放方法如下: 使用Previ ...

  4. WPF中TextBox更改完了之后进行操作

    WPF中TextBox的Text更改的相关方法有两种 TextChanged SourceUpdated TextChanged 事件 在 TextBox 控件中的文本发生更改时使用 TextChan ...

  5. WPF 实现TextBox背景中提示文字(水印效果)

    网上找到的多是使用VisualBrush,代码如下.单独使用方便,但加入样式模板文字内容无法绑定附加属性(或依赖属性). <TextBox HorizontalAlignment="C ...

  6. 『WPF』TextBox元素过滤键盘输入

    本文最后更新于 2019年 5月 6号 凌晨 2点 03分,并同步发布于 : 简书 -- 创作你的创作 CSDN -- 专业 IT 技术社区 www.tobinary.art -- 我的博客 在编写 ...

  7. WPF 4 TextBox 笔刷特效

    TextBox 控件是我们开发过程中必不可少的组件,它可以使应用程序方便的与用户进行文字交互.在新WPF 4 中又为TextBox 添加了两种新笔刷特效:Selection .Caret ,通过这两种 ...

  8. WPF清空textbox文本框

    1. 第一步:将每个窗体当中的网格布局控件--Grid命名 2. 3. 4. 5. 第二步:在UI层,添加清空的类,以及判空的类 6. 7. 第三步:在两个类里面添加命名空间 8. 9. 这两个引用是 ...

  9. C# WPF实现TextBox屏蔽非法字符

    本篇文章总结一下,如何在WPF中,通过PreviewKeyDown事件,实现在TextBox中只允许输入数字,屏蔽非法字符. 以前用Winform时,我习惯用KeyPress事件管控TextBox中的 ...

最新文章

  1. 消息队列—经典5连问—你能抗几道?
  2. allow_unreachable flag
  3. CSP认证201604-1 折点计数[C++题解]:枚举、遍历
  4. axure怎么转换成php,墨刀和axure的区别是什么?
  5. nssl1304-最大正方形【二分答案】
  6. linux 下清空回收站命令
  7. 程序员想早点下班被经理拒绝,一气之下要辞职,经理慌了
  8. android升级异常,升级AndroidX遇到的问题
  9. 瞧瞧,这样的代码才叫 Pythonic
  10. Apache Flink 零基础入门教程(六):状态管理及容错机制
  11. backtrack常用的一些综合扫描工具实例用法
  12. 百度网盘为何下载总是那么慢?如何才能实现快速下载?
  13. python汇率换算程序_Python编写一个汇率兑换程序
  14. SOA对话:金融风暴不会阻碍SOA市场发展
  15. Python 虚拟环境迁移
  16. 使用form表单和table表格制作个人简历
  17. Python-爬取今日头条美图
  18. 最新的单片机_关于单片机通过蓝牙将数据传输给手机并在app上面显示出来怎么实现...
  19. 1月8日,30秒知全网,精选7个热点
  20. vb6DataGrid

热门文章

  1. 深拷贝和浅拷贝的理解-----------【Java基础】
  2. 《Python网络爬虫实战》读书笔记1
  3. 众昂矿业:萤石在化工行业领域用途解析
  4. Java连接MySQL数据库时的时区问题
  5. 测试用例的设计方法:边界值分析法
  6. 程序员告诉你:C/C++后台开发需要学习哪些技能书
  7. jquery.validate.js 详解
  8. 干货丨致研究者:2018 AI 研究趋势
  9. 常见的几种限流算法代码实现(JAVA)
  10. 华为mate x鸿蒙系统,华为Mate X2发布:4月起首批升级鸿蒙系统