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. Bitnami --https://bitnami.com/stacks

    Bitnami is an app store for server software. Install your favorite applications in your own servers ...

  2. C#之文本操作

    [转载]C#文件操作大全(SamWang) 文件与文件夹操作主要用到以下几个类: 1.File类: 提供用于创建.复制.删除.移动和打开文件的静态方法,并协助创建 FileStream 对象. msd ...

  3. 使用宏批量将多个csv文件转成excel文件

    在一个压缩文件中有100多个csv文件,要求要将此100多个csv文件转成excel文件,名字命名不变,有三种方式: 1. 傻不拉几的复制粘贴法 2. 一个一个打开csv文件,另存为xls文件,工作量 ...

  4. ABAP 内表的行列转换-发货通知单-打印到Excel里-NEW

    *********************************************************************** * Title           : ZSDF002  ...

  5. SQL触发器中若取到null值可能引发的问题

    declare @code varchar(20), @cs varchar(20),@zc varchar(20)set @cs='('+@cs+'*'+@zc+')'print '字符'+@csi ...

  6. 用 get 同步/异步 方式获取网络数据并输出

    //同步请求 //创建NSString用来存储请求的网址 NSString* str=@"http://v.juhe.cn/weather/index?format=2&cityna ...

  7. assign() 方法

    assign() 方法可加载一个新的文档. 语法 location.assign(URL) <html> <head> <script type="text/j ...

  8. modbus协议讲义

        Modbus 一个工业上常用的通讯协议.一种通讯约定.Modbus协议包括RTU.ASCII.TCP.其中MODBUS-RTU最常用,比较简单,在单片机上很容易实现.虽然RTU比较简单,但是看 ...

  9. LightOJ1336 Sigma Function(约数和为偶数的个数)

    Sigma Function Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit ...

  10. 编写一个程序,求s=1+(1+2)+(1+2+3)+…+(1+2+3+…+n)的值

    编写一个程序,求s=1+(1+2)+(1+2+3)+…+(1+2+3+…+n)的值 1 #import <Foundation/Foundation.h>  2   3 int main( ...