和之前一样,先来看看效果:

  

  这个TextBox可设置水印,可设置必填和正则表达式验证。

  验证?没错,就是验证! 就是在输入完成后,控件一旦失去焦点就会自动验证!会根据我开放出来的“是否可以为空”属性进行验证,一旦为空,则控件变为警告样式。

  但这还不是最特别的,为了各种手机号啊,邮箱啊的验证,我还开放了一个正则表达式的属性,在这个属性中填上正则表达式,同上, 一旦失去焦点就会自动验证输入的内容能否匹配正则表达式,如果不能匹配,则控件变为警告样式。

  之后,代码还可以通过我开放的另一个属性来判断当前输入框的输入是否有误!

  好了,来看代码吧:

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ctrl="clr-namespace:KAN.WPF.XCtrl.Controls">
<Style TargetType="{x:Type ctrl:XTextBox}">
<!--StyleFocusVisual在上一篇里说了-->
<Style.Resources>
<ResourceDictionary Source="/KAN.WPF.Xctrl;component/Themes/CommonStyle.xaml"/>
</Style.Resources>
<Setter Property="FocusVisualStyle" Value="{StaticResource StyleFocusVisual}"/>
<Setter Property="BorderBrush" Value="Silver"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ctrl:XTextBox}">
<Border Name="brdText" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}" SnapsToDevicePixels="true" Padding="2">
<Grid>
<ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
<StackPanel Orientation="Horizontal" Visibility="Collapsed" Name="stpWatermark">
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Center"
FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}"
Foreground="{Binding XWmkForeground, RelativeSource={RelativeSource TemplatedParent}}"
Text="{Binding XWmkText, RelativeSource={RelativeSource TemplatedParent}}" Cursor="IBeam" />
</StackPanel>
<ContentPresenter></ContentPresenter>
</Grid>
</Border>
<ControlTemplate.Triggers>
<!--当失去焦点并且没有输入任何内容时-->
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Text" Value=""/>
<Condition Property="IsFocused" Value="False"/>
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter Property="Visibility" TargetName="stpWatermark" Value="Visible"/>
</MultiTrigger.Setters>
</MultiTrigger>
<!--当验证失败时-->
<Trigger Property="XIsError" Value="true">
<Setter TargetName="brdText" Property="BorderBrush" Value="Red" />
<Setter TargetName="brdText" Property="Background" Value="Beige" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>

  再来看看CS:

 using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Input;
using System.Text.RegularExpressions; namespace KAN.WPF.XCtrl.Controls
{
/// <summary>
/// 扩展输入框:可设置水印,可设置必填,可设置正则表达式验证
/// </summary>
public class XTextBox:TextBox
{
#region 依赖属性
public static readonly DependencyProperty XWmkTextProperty;//水印文字
public static readonly DependencyProperty XWmkForegroundProperty;//水印着色
public static readonly DependencyProperty XIsErrorProperty;//是否字段有误
public static readonly DependencyProperty XAllowNullProperty;//是否允许为空
public static readonly DependencyProperty XRegExpProperty;//正则表达式
#endregion #region 内部方法
/// <summary>
/// 注册事件
/// </summary>
public XTextBox()
{
this.LostFocus += new RoutedEventHandler(XTextBox_LostFocus);
this.GotFocus += new RoutedEventHandler(XTextBox_GotFocus);
this.PreviewMouseDown += new MouseButtonEventHandler(XTextBox_PreviewMouseDown);
} /// <summary>
/// 静态构造函数
/// </summary>
static XTextBox()
{
//注册依赖属性
XTextBox.XWmkTextProperty = DependencyProperty.Register("XWmkText", typeof(String), typeof(XTextBox), new PropertyMetadata(null));
XTextBox.XAllowNullProperty = DependencyProperty.Register("XAllowNull", typeof(bool), typeof(XTextBox), new PropertyMetadata(true));
XTextBox.XIsErrorProperty = DependencyProperty.Register("XIsError", typeof(bool), typeof(XTextBox), new PropertyMetadata(false));
XTextBox.XRegExpProperty = DependencyProperty.Register("XRegExp", typeof(string), typeof(XTextBox), new PropertyMetadata(""));
XTextBox.XWmkForegroundProperty = DependencyProperty.Register("XWmkForeground", typeof(Brush),
typeof(XTextBox), new PropertyMetadata(Brushes.Silver));
FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(XTextBox), new FrameworkPropertyMetadata(typeof(XTextBox)));
} /// <summary>
/// 失去焦点时检查输入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void XTextBox_LostFocus(object sender, RoutedEventArgs e)
{
this.XIsError = false;
if (XAllowNull == false && this.Text.Trim() == "")
{
this.XIsError = true;
}
if (Regex.IsMatch(this.Text.Trim(), XRegExp) == false)
{
this.XIsError = true;
}
} /// <summary>
/// 获得焦点时选中文字
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void XTextBox_GotFocus(object sender, RoutedEventArgs e)
{
this.SelectAll();
} /// <summary>
/// 鼠标点击时选中文字
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void XTextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (this.IsFocused == false)
{
TextBox textBox = e.Source as TextBox;
textBox.Focus();
e.Handled = true;
}
}
#endregion #region 公布属性
/// <summary>
/// 公布属性XWmkText(水印文字)
/// </summary>
public String XWmkText
{
get
{
return base.GetValue(XTextBox.XWmkTextProperty) as String;
}
set
{
base.SetValue(XTextBox.XWmkTextProperty, value);
}
} /// <summary>
/// 公布属性XWmkForeground(水印着色)
/// </summary>
public Brush XWmkForeground
{
get
{
return base.GetValue(XTextBox.XWmkForegroundProperty) as Brush;
}
set
{
base.SetValue(XTextBox.XWmkForegroundProperty, value);
}
} /// <summary>
/// 公布属性XIsError(是否字段有误)
/// </summary>
public bool XIsError
{
get
{
return (bool)base.GetValue(XTextBox.XIsErrorProperty);
}
set
{
base.SetValue(XTextBox.XIsErrorProperty, value);
}
} /// <summary>
/// 公布属性XAllowNull(是否允许为空)
/// </summary>
public bool XAllowNull
{
get
{
return (bool)base.GetValue(XTextBox.XAllowNullProperty);
}
set
{
base.SetValue(XTextBox.XAllowNullProperty, value);
}
} /// <summary>
/// 公布属性XRegExp(正则表达式)
/// </summary>
public string XRegExp
{
get
{
return base.GetValue(XTextBox.XRegExpProperty) as string;
}
set
{
base.SetValue(XTextBox.XRegExpProperty, value);
}
}
#endregion
}
}

  怎么样?还算不错吧!我觉得这个控件的用处算是最大的了!用上这个和上一篇的Button基本可以完成很多WPF项目了!

  不过~好像还少了个主窗体!没错!下一篇就来说说怎么自定义主窗体!

  有疑问的多留言哟!

WPF自定义控件(二)——TextBox的更多相关文章

  1. WPF自定义控件二:Border控件与TextBlock控件轮播动画

    需求:实现Border轮播动画与TextBlock动画 XAML代码如下: <Window.Resources> <Storyboard x:Key="OnLoaded1& ...

  2. WPF自定义控件与样式(3)-TextBox & RichTextBox & PasswordBox样式、水印、Label标签、功能扩展

    一.前言.预览 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要是对文本 ...

  3. 【转】WPF自定义控件与样式(3)-TextBox & RichTextBox & PasswordBox样式、水印、Label标签、功能扩展

    一.前言.预览 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要是对文本输入控件进行样式开发,及相关扩展功能开发,主要内容包括: 基本文 ...

  4. WPF自定义控件(二)の重写原生控件样式模板

    话外篇: 要写一个圆形控件,用Clip,重写模板,去除样式引用圆形图片可以有这三种方式. 开发过程中,我们有时候用WPF原生的控件就能实现自己的需求,但是样式.风格并不能满足我们的需求,那么我们该怎么 ...

  5. 工作记录--WPF自定义控件,实现一个可设置编辑模式的TextBox

    原文:工作记录--WPF自定义控件,实现一个可设置编辑模式的TextBox 1. 背景 因为最近在使用wpf开发桌面端应用,在查看页面需要把TextBox和Combox等控件设置为只读的.原本是个很简 ...

  6. WPF自定义控件与样式(1)-矢量字体图标(iconfont)

    一.图标字体 图标字体在网页开发上运用非常广泛,具体可以网络搜索了解,网页上的运用有很多例子,如Bootstrap.但在C/S程序中使用还不多,字体图标其实就是把矢量图形打包到字体文件里,就像使用一般 ...

  7. WPF自定义控件与样式(2)-自定义按钮FButton

    一.前言.效果图 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 还是先看看效果 ...

  8. WPF自定义控件与样式(15)-终结篇 & 系列文章索引 & 源码共享

    系列文章目录  WPF自定义控件与样式(1)-矢量字体图标(iconfont) WPF自定义控件与样式(2)-自定义按钮FButton WPF自定义控件与样式(3)-TextBox & Ric ...

  9. WPF自定义控件与样式(4)-CheckBox/RadioButton自定义样式

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: Che ...

  10. WPF自定义控件与样式(5)-Calendar/DatePicker日期控件自定义样式及扩展

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 日历控 ...

随机推荐

  1. 【Android 界面效果26】listview android:cacheColorHint,android:listSelector属性作用

    ListView是常用的显示控件,默认背景是和系统窗口一样的透明色,如果给ListView加上背景图片,或者背景颜色时,滚动时listView会黑掉, 原因是,滚动时,列表里面的view重绘时,用的依 ...

  2. 利用Android手机里的摄像头进行拍照

    ------- 源自梦想.永远是你IT事业的好友.只是勇敢地说出我学到! ---------- 1.在API Guides中找到Camera,里面讲解了如何使用系统自带的摄像头进行工作,之后我会试着翻 ...

  3. Jqeury获取table当前行与指定列

    今天遇到了一个Jqeury获取table当前行与指定列的问题: 大概的实现要求是一个页面中,上面有几个input输入框,下面有一个table,当在输入框中输入内容的时候,点击添加按钮的时候,在下面ta ...

  4. Android学习笔记⑥——UI组件的学习ImageView相关

    ImageView是集成了View的组件,它的主要工作就是显示一些图片啊,虽然他的用法一句话概括了,但是我觉得学起来应该不会太简单,正所谓 短小而精悍么 :) ImageView 派生了 ImageB ...

  5. 通过maven添加quartz

    pom.xml中相关dependency信息 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <depen ...

  6. web前端开发(6)

    为了避免全局变量泛滥导致冲突,最简单有效的办法是用匿名函数将脚本包起来,让变量的作用域控制在函数之内.

  7. ionic使用sass

    sass 是一个css的预编译器,常见的预编译器有less,sass,stylus等,目前sass似乎更受青睐一些,bootstrap的最新版本以及ionic 都是用sass来构建页面效果的.这篇文章 ...

  8. java处理高并发高负载类网站的优化方法

    java处理高并发高负载类网站中数据库的设计方法(java教程,java处理大量数据,java高负载数据) 一:高并发高负载类网站关注点之数据库 没错,首先是数据库,这是大多数应用所面临的首个SPOF ...

  9. 继承Animation

    package cativity.cyq.alphaanimal; import android.view.animation.Animation; import android.view.anima ...

  10. android sdk manager 无法更新

    1.在C:\Windows\System32\drivers\etc找到Hosts文件用记事本打开,在最末尾添加如下代码,保存关闭: #Google主页203.208.46.146 www.googl ...