WPF自定义数字输入框控件

要求:只能输入数字和小数点,可以设置最大值,最小值,小数点前长度,小数点后长度(支持绑定设置);

代码如下:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input; namespace MyTool
{
/// <summary>
/// 数字输入框
/// author lixinmiao
/// </summary>
class NumTextBox : TextBox
{
public static readonly DependencyProperty NumTypeProperty = DependencyProperty.Register("NumType", typeof(Type), typeof(NumTextBox));
public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(decimal), typeof(NumTextBox), new FrameworkPropertyMetadata(
new PropertyChangedCallback(CheckProperty))); public static readonly DependencyProperty MinValueProperty = DependencyProperty.Register("MinValue", typeof(decimal), typeof(NumTextBox), new FrameworkPropertyMetadata(
new PropertyChangedCallback(CheckProperty)));
public static readonly DependencyProperty PointLenthProperty = DependencyProperty.Register("PointLenth", typeof(int), typeof(NumTextBox));
public static readonly DependencyProperty NumberLengthProperty = DependencyProperty.Register("NumberLength", typeof(int), typeof(NumTextBox));
// private System.Globalization.CultureInfo cI;
public NumTextBox()
{
MinValue = decimal.MinValue;
MaxValue = decimal.MaxValue;
PointLenth = ;
NumberLength = ; }
public enum Type { Decimal, UDecimal, Int, UInt }
/// <summary>
/// 设置或获取textbox的输入数据类型
/// </summary>
public Type NumType
{
get { return (Type)GetValue(NumTypeProperty); }
set { SetValue(NumTypeProperty, value); }
}
/// <summary>
/// 设定或获取最大值
/// </summary>
public decimal MaxValue
{
get { return (decimal)GetValue(MaxValueProperty); }
set { SetValue(MaxValueProperty, value); }
} /// <summary>
/// 设定或获取最小值
/// </summary>
public decimal MinValue
{
get { return (decimal)GetValue(MinValueProperty); }
set { SetValue(MinValueProperty, value); }
}
/// <summary>
/// 设置或获取小数点前的位数
/// </summary>
public int NumberLength
{
get { return (int)GetValue(NumberLengthProperty); }
set { SetValue(NumberLengthProperty, value); }
}
/// <summary>
/// 设置或获取小数点后位数长度
/// </summary>
public int PointLenth
{
get { return (int)GetValue(PointLenthProperty); }
set { SetValue(PointLenthProperty, value); }
} private string val = "";
/// <summary>
/// 设定最大值最小值依赖属性回调函数
/// </summary>
/// <param name="d"></param>
/// <param name="e"></param>
private static void CheckProperty(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
NumTextBox ntb = d as NumTextBox;
if (ntb.MaxValue < ntb.MinValue)
ntb.MaxValue = ntb.MinValue;
}
/// <summary>
/// 重写KeyDown事件,提供与事件有关的数据,过滤输入数据格式
/// </summary>
/// <param name="e"></param>
protected override void OnKeyDown(KeyEventArgs e)
{
string txt = this.Text;
int ind = this.CaretIndex;
if (txt.Contains("."))
{
// Console.WriteLine(txt.Split('.')[0] + "he " + txt.Split('.')[1]);
if (txt.Split('.')[].Length >= PointLenth && ind > txt.Split('.')[].Length && this.SelectionLength == )//控制小数点后输入位数
{
e.Handled = true;
return;
}
else if (txt.Split('.')[].Length >= NumberLength && ind <= txt.Split('.')[].Length)//控制小数点前输入位数(有小数点)
{
e.Handled = true;
return;
}
}
else if (txt.Length == NumberLength && e.Key != Key.Decimal && e.Key != Key.OemPeriod)//控制小数点前输入位数(无小数点)
{
e.Handled = true;
return;
} if (e.Key == Key.Decimal || e.Key == Key.OemPeriod)
{
val = ".";
}
else
{ val = "";
}
switch (NumType)
{ case Type.UInt:
//屏蔽非法按键
if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key.ToString() == "Tab")
{
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;
if (e.Key.ToString() != "RightCtrl")
{ }
}
break;
case Type.Int:
//屏蔽非法按键
if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Subtract || e.Key.ToString() == "Tab")
{
if ((txt.Contains("-") || this.CaretIndex != ) && e.Key == Key.Subtract)
{
e.Handled = true;
return;
}
e.Handled = false;
}
else if (((e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemMinus) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
{
if ((txt.Contains("-") || this.CaretIndex != ) && e.Key == Key.OemMinus)
{
e.Handled = true;
return;
}
e.Handled = false;
}
else
{
e.Handled = true;
if (e.Key.ToString() != "RightCtrl")
{ }
}
break;
case Type.Decimal:
//屏蔽非法按键
if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal || e.Key == Key.Subtract || e.Key.ToString() == "Tab")
{
if (txt.Contains(".") && e.Key == Key.Decimal)
{
e.Handled = true;
return;
}
else if ((txt.Contains("-") || this.CaretIndex != ) && e.Key == Key.Subtract)
{
e.Handled = true;
return;
}
e.Handled = false;
}
else if (((e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemPeriod || e.Key == Key.OemMinus) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
{
if (txt.Contains(".") && e.Key == Key.OemPeriod)
{
e.Handled = true;
return;
}
else if ((txt.Contains("-") || this.CaretIndex != ) && e.Key == Key.OemMinus)
{
e.Handled = true;
return;
}
e.Handled = false;
}
else
{
e.Handled = true;
if (e.Key.ToString() != "RightCtrl")
{ }
}
break;
case Type.UDecimal:
default:
//屏蔽非法按键
if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal || e.Key.ToString() == "Tab")
{
if (txt.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 (txt.Contains(".") && e.Key == Key.OemPeriod)
{
e.Handled = true;
return;
}
e.Handled = false;
}
else
{
e.Handled = true;
if (e.Key.ToString() != "RightCtrl")
{ }
} break;
}
base.OnKeyDown(e);
}
/// <summary>
///粘贴内容过滤,设定最大值、最小值,限制小数点输入长度
/// </summary>
/// <param name="e"></param>
protected override void OnTextChanged(TextChangedEventArgs e)
{
int t1 = this.Text.Length;
if (t1 != )//用于是否可以将文本空置空
{
decimal d = ;
if (this.Text != "-" && this.Text != "." && this.Text != "-0" && this.Text != "-." && this.Text != "-0." && val != ".")
{
if (decimal.TryParse(this.Text, out d))
{
if (NumType == Type.Decimal || NumType == Type.UDecimal)
{
if (d.ToString().Split('.')[].Length > NumberLength)//
{
d = ;
}
else
{
d = Math.Round(d, PointLenth, MidpointRounding.AwayFromZero);
}
}
else
if (d.ToString().Split('.')[].Length > NumberLength)//
{
d = ;
}
else
{
d = Math.Round(d, , MidpointRounding.AwayFromZero);
}
}
int t2 = d.ToString().Length;
if (Math.Abs(t1 - d.ToString().Length) > )
{
this.Text = d.ToString();
this.CaretIndex = d.ToString().Length;
}
else
{
this.Text = d.ToString();
} }
if ((NumType == Type.UDecimal || NumType == Type.UInt) && this.Text.Contains("-"))
{
this.Text = Math.Abs(d).ToString();
}
if ((NumType == Type.UInt || NumType == Type.Int) && this.Text.Contains("."))
{
this.Text = int.Parse(d.ToString()).ToString();
}
}
base.OnTextChanged(e);
} /// <summary>
///如果数据是0,得到光标,清空数据
/// </summary>
/// <param name="e"></param>
protected override void OnGotFocus(RoutedEventArgs e)
{
//InputMethod.SetPreferredImeState(this, InputMethodState.Off);
//cI = InputLanguageManager.GetInputLanguage(this);
//decimal d = 0;
//if (decimal.TryParse(this.Text, out d))
//{
if (this.Text == "")
{
this.Text = "";
}
//}
//else
//{
// this.Text = "";
//}
base.OnGotFocus(e);
} /// <summary>
///失去光标,确定最大值最小值
/// </summary>
/// <param name="e"></param>
protected override void OnLostFocus(RoutedEventArgs e)
{
decimal d = ;
if (decimal.TryParse(this.Text, out d))
{
if (d < MinValue)
{
d = MinValue;
this.Text = d.ToString();
} else if (d > MaxValue)
{
d = MaxValue;
this.Text = d.ToString();
}
}
else if (string.IsNullOrEmpty(this.Text))
{
this.Text = "";
}
else
{
this.Text = d.ToString();
}
base.OnLostFocus(e);
//System.Globalization.CultureInfo cI = InputLanguageManager.GetInputLanguage(this);
//InputMethod.SetPreferredImeState(this, InputMethodState.DoNotCare);
//InputLanguageManager.SetInputLanguage(this, cI);
//cI = null;
}
}
}

具体用法:

将改文件命名控件引用到要使用的xaml页面,然后采用引用控件的使用方法使用即可。
通过NumType设置绑定的数据类型,整数,正整数,小数,正小数;

其余的几个属性类似;

欢迎探讨。

有问题,请联系QQ:2860580831 或发邮件到2860580831@qq.com

上一篇:asp.net 动态添加自定义控件


下一篇:UVALive 3027 并查集