customTextbox
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Input; namespace MyWPFCustomControls
{
public class CustomTextBox : TextBox
{
public CustomTextBox()
{ } static CustomTextBox()
{ }
public CustomTextBoxType CustomTextBoxType
{
get { return (CustomTextBoxType)GetValue(CustomTextBoxTypeProperty); }
set { SetValue(CustomTextBoxTypeProperty, value); }
} // Using a DependencyProperty as the backing store for CustomTextBoxType. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CustomTextBoxTypeProperty =
DependencyProperty.Register("CustomTextBoxType", typeof(CustomTextBoxType), typeof(CustomTextBox), new UIPropertyMetadata(CustomTextBoxType.None, new PropertyChangedCallback(OnCustomTextBoxTypeChanged))); private static void OnCustomTextBoxTypeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var tb = obj as CustomTextBox; } public override void OnApplyTemplate()
{
base.OnApplyTemplate();
}
protected override void OnPreviewTextInput(System.Windows.Input.TextCompositionEventArgs e)
{
base.OnPreviewTextInput(e);
}
protected override void OnTextChanged(TextChangedEventArgs e)
{
base.OnTextChanged(e);
//屏蔽中文输入和非法字符粘贴输入 TextChange[] change = new TextChange[e.Changes.Count];
e.Changes.CopyTo(change, ); int offset = change[].Offset;
if (change[].AddedLength > )
{
switch (this.CustomTextBoxType)
{
case CustomTextBoxType.None:
break;
case CustomTextBoxType.Integer:
NonIntergerHandle(change, offset);
break;
case CustomTextBoxType.Decimal:
NonDecimalHandle(change, offset);
break;
case CustomTextBoxType.Alphabet:
break;
case CustomTextBoxType.Alphanumeric:
break;
case CustomTextBoxType.Currency:
break;
default:
break;
} }
} private void NonDecimalHandle(TextChange[] change, int offset)
{
Decimal num = ;
if (!Decimal.TryParse(this.Text, out num))
{
this.Text = this.Text.Remove(offset, change[].AddedLength);
this.Select(offset, );
}
} private void NonIntergerHandle(TextChange[] change, int offset)
{
long num = ;
if (!long.TryParse(this.Text, out num))
{
this.Text = this.Text.Remove(offset, change[].AddedLength);
this.Select(offset, );
}
} protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
{
base.OnPreviewKeyDown(e);
switch (this.CustomTextBoxType)
{
case CustomTextBoxType.None:
break;
case CustomTextBoxType.Integer:
IntegerOperation(e);
break;
case CustomTextBoxType.Decimal:
DecimalOperation(e);
break;
case CustomTextBoxType.Alphabet:
break;
case CustomTextBoxType.Alphanumeric:
break;
case CustomTextBoxType.Currency: break;
default:
break;
} } private void DecimalOperation(System.Windows.Input.KeyEventArgs e)
{
if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal)
{ if (this.Text.Contains(".") && e.Key == Key.Decimal)
{
e.Handled = true;
return;
}
e.Handled = false;
}
else if (((e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemPeriod) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
{
if (this.Text.Contains(".") && e.Key == Key.OemPeriod)
{
e.Handled = true;
return;
}
e.Handled = false;
}
else
{
e.Handled = true;
}
} private void IntegerOperation(System.Windows.Input.KeyEventArgs e)
{
if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9))
{
e.Handled = false;
}
else if ((e.Key >= Key.D0 && e.Key <= Key.D9) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
} public enum CustomTextBoxType
{
None,
Integer,
Decimal,
Alphabet,
Alphanumeric,
Currency
}
}
customTextbox的更多相关文章
- 准备.Net转前端开发-WPF界面框架那些事,UI快速实现法
		
题外话 打开博客园,查看首页左栏的”推荐博客”,排名前五的博客分别是(此处非广告):Artech.小坦克.圣殿骑士.腾飞(Jesse).数据之巅.再看看它们博客的最新更新时间:Artech(2014- ...
 - ASP.NET页面生存周期
		
.Net的Page请求过程:
 
随机推荐
- Django~static files
			
such as images, JavaScript, or CSS https://docs.djangoproject.com/en/1.9/howto/static-files/ django. ...
 - “假学习“&”真学习“?(摘)
			
什么叫做“假学习”? 一.看书 买一堆书,有空看看.看书,这是典型的假学习.看书看不懂还在看,就是假学习,欺骗自己,安慰自己正在学习而已.专业书都写得很好,但大都是写给已经懂的人看的.看书的最大作用就 ...
 - 【python】getopt使用
			
来源:http://blog.chinaunix.net/uid-21566578-id-438233.html 注意对比:[python]argparse模块 作者:limodou版权所有limod ...
 - 安装oracleASM
			
问题:oracleasm status不能启动 解决方案: [root@localhost ~]# oracleasm statusChecking if ASM is loaded: noCheck ...
 - js闭包原理
			
一.定义 官方解释:闭包是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. ****定义在函数中的函数,并且可在外部访问得到.(正常情况下我们是无法 ...
 - July 6th, Week 28th Wednesday, 2016
			
Diligence is the mother of good fortune. 勤勉是好运之母. The mother of good fortune can be diligence, conti ...
 - iOS 动态计算文本内容的高度
			
关于ios 下动态计算文本内容的高度,经过查阅和网上搜素,现在看到的有以下几种方法: 1. // 获取字符串的大小 ios6 - (CGSize)getStringRect_:(NSString* ...
 - Java使用正则表达式解析LRC歌词文件
			
LRC歌词是一种应用广泛的歌词文件,各主流播放器都支持. lrc歌词文本中含有两类标签: 1.标识标签(ID-tags) [ar:艺人名] [ti:曲名] [al:专辑名] [by:编者(指编辑LRC ...
 - 三、jQuery--jQuery基础--jQuery基础课程--第3章 jQuery过滤性选择器
			
1.:first过滤选择器 本章我们介绍过滤选择器,该类型的选择器是根据某过滤规则进行元素的匹配,书写时以“:”号开头,通常用于查找集合元素中的某一位置的单个元素. 在jQuery中,如果想得到一组相 ...
 - 登录到mysql查看binlog日志
			
查看当前第一个binlog文件的内容 show binlog events; 查看指定binlog文件内容 show binlog events in 'mysql-bin.000002'; 查看当前 ...