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请求过程:
随机推荐
- fib数列变种题目
对一个正整数作如下操作:如果是偶数则除以2,如果是奇数则加1,如此进行直到1时操作停止,求经过9次操作变为1的数有多少个? 第9次操作:结果1由2产生.1个被操作数8:结果2只能由4产生.1个被操作数 ...
- linux expect
1.首先确定是否安装expect /home/root> which expect /usr/bin/expect 如果没有安装,先安装一下 安装方法: 请参考 http://www.cnblo ...
- Innodb 表空间卸载、迁移、装载
从MySQL的Innodb特性中我们知道,Inndob的表空间有共享和独享的特点,如果是共享的.则默认会把表空间存放在一个文件中(ibdata1),当开启独享表空间参数Innodb_file_per_ ...
- 【leetcode】Binary Tree Level Order Traversal I & II
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- DB2中错误信息说明
DB2错误信息SQLCODE SQLSTATE (按sqlcode排序) .分类: db2数据库 2012-10-19 11:35 2942人阅读 评论(0) 收藏 举报 db2sql存储table数 ...
- IOS - UITableView分批显示数据 实现点击加载更多
Phone屏幕尺寸是有限的,如果需要显示的数据很多,可以先数据放到一个table中,先显示10条,table底部有一察看更多选项,点击察看更多查看解析的剩余数据.基本上就是数据源里先只放10条, 点击 ...
- 宠物收养所(bzoj1208)
Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...
- Lambda表达式与标准运算符查询
class Program { static void Main(string[] args) { //Lambda表达式输出List集合每一项 List<string> list = n ...
- Quartus 11.0 的AS 下载方式和JTAG下载jic文件的方式
FPGA下载的三种方式:主动配置方式(AS)和被动配置方式(PS)和最常用的(JTAG)配置方式: AS由FPGA器件引导配置操作过程,它控制着外部存储器和初始化过程,EPCS系列.如EPCS1,EP ...
- Java -- File
@.getPath().getAbsolutePath().getCanonicalPath()区别 原文:http://blog.csdn.net/wh_19910525/article/detai ...