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的更多相关文章

  1. 准备.Net转前端开发-WPF界面框架那些事,UI快速实现法

    题外话 打开博客园,查看首页左栏的”推荐博客”,排名前五的博客分别是(此处非广告):Artech.小坦克.圣殿骑士.腾飞(Jesse).数据之巅.再看看它们博客的最新更新时间:Artech(2014- ...

  2. ASP.NET页面生存周期

    .Net的Page请求过程:

随机推荐

  1. Java GUI学习笔记之初识AWT和Swing

    Frame f = new Frame(); //获取显示器的尺寸 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize() ...

  2. iOS 动画结束后 view的位置 待完善

    默认的动画属性,动画结束后,view会回到原始位置.但是如果设定了 CAAnimation的 removedOnCompletion 属性,那么view会保持这个位置!但是真实的接收 点击的frame ...

  3. VS2010工程文件减肥

    由于VS2010中新增加了sdf和ipch文件等浏览数据库来支持智能浏览感知编辑.显示类视图等,使得随便一个小工程就上百兆,很占用空间也不方便工程项目的打包备份.为了不使用数据库以减小VS2010中的 ...

  4. delphi cxgrid 使用方法

    delphi cxgrid 使用方法1.绑定数据 方法 cxGrid1DBTableView1.DataController.DataSource:=DataSource12.去掉"Drag ...

  5. ACM/ICPC 之 Prim范例(ZOJ1586-POJ1789(ZOJ2158))

    两道Prim解法范例题型,简单的裸Prim,且两题相较以边为重心的Kruskal解法而言更适合以点为重心扩展的Prim解法. ZOJ1586-QS Network 题意:见Code 题解:直接的MST ...

  6. CentOS 6.6 (Desktop)部署Apache、MySQL以及Eclipse Luna等记录

    内容较多,持续更新(2015-03-12 16:37:05) *如果没有特别说明,以下操作都是在root账号下完成,图形界面为GNOME. 一.防火墙 先从防火墙入手,为了后续的环境搭建,需要打开80 ...

  7. C# 对象深度拷贝

    转载 using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using ...

  8. jquery $(document).ready() 与window.onload的异同

    Jquery中$(document).ready()的作用类似于传统JavaScript中的window.onload方法,不过与window.onload方法还是有区别的.   1.执行时间     ...

  9. September 21st 2016 Week 39th Wednesday

    Don't try so hard, the best things come when you least expect them. 不要着急,最好的总会在最不经意的时候出现. Always tur ...

  10. September 8th 2016 Week 37th Thursday

    The secret of high-impact business is early preparation. 高效商务,赢在未雨绸缪. Early and best preparation is ...