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 CRM __contains与__icontains区别
http://www.yihaomen.com/article/python/199.htm operators = { 'exact': '= %s', 'iexact': 'LIKE %s', ' ...
- Qt不同类之间信号槽连接
1.类必须继承QObject. #ifndef TESTA_H #define TESTA_H #include <QObject> class TestA : public QObjec ...
- Enum:EXTENDED LIGHTS OUT(POJ 1222)
亮灯 题目大意:有一个5*6的灯组,按一盏灯会让其他上下左右4栈和他自己灯变为原来相反的状态,要怎么按才会把所有的灯都按灭? 3279翻版题目,不多说,另外这一题还可以用其他方法,比如DFS,BFS, ...
- log4net使用
平时项目里一直都有在使用log4net作为日志记录模块,当时一直都没有去理解log4net的配置文件信息.今天抽出了一点时间来看了看配置文件信息. log4net配置文件信息: <log4net ...
- POJ 3597 Polygon Division (DP)
题目链接 题意:把一个正多边形分成数个三角形或者四边形,问有多少种方案. 题解: 如果分出的全为三角形的话,那就是正多边形三角剖分问题.它的结果就是Catalan数.现在也可以划分出四边形的话,可以采 ...
- MyEclipse/Eclipse中修改包的显示结构
操作如下:
- IOS - ARC改为非ARC
1.project -> Build settings -> Apple LLVM complier 3.0 - Language -> objective-C Automatic ...
- mysql常用函数整理
一.数学函数 数学函数主要用于处理数字,包括整型.浮点数等. ABS(x) 返回x的绝对值 ) -- 返回1 CEIL(x),CEILING(x) 返回大于或等于x的最小整数 SELECT CEIL( ...
- python基础——返回函数
python基础——返回函数 函数作为返回值 高阶函数除了可以接受函数作为参数外,还可以把函数作为结果值返回. 我们来实现一个可变参数的求和.通常情况下,求和的函数是这样定义的: def calc_ ...
- CSS3混合模式background-blend-mode
注意:background属性中的背景图片和颜色混合,只能在一个background属性中. 属性值: background-blend-mode: normal; //正常 background-b ...