本文介绍了我将如何在文本框样式模板WPF winform中设置minlength属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我的问题是我要在wpf文本框中验证平底锅编号min 10 char和max 10,
如果不是则停止按任意键。 ..
我尝试了什么:
如何绑定文本框绑定事件到datagrid ..
Hi All,
My Problem is i want to validate pan number in wpf textbox min 10 char and max 10,
If Not then restict to press any key...
What I have tried:
how to bind textbox binding event to datagrid..
推荐答案
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BODCommon;
using ObjectExtensions;
namespace WPFCommon.Controls
{
/// <summary>
///
/// </summary>
public class TextBoxNumeric : TextBox
{
#region Enumerators
/// <summary>
/// The types of numeric values that can be entered with this control
/// </summary>
public enum NumericTypes
{
/// <summary>
/// Any integer value (see comments for GetNumericValue nmethods)
/// </summary>
Integer,
/// <summary>
/// Any decimal value
/// </summary>
Decimal,
/// <summary>
/// Any floating point value (see comments for GetNumericValue nmethods)
/// </summary>
Double
}
#endregion Enumerators
#region Fields
private bool eventHooked = false;
private string thisText = string.Empty;
#endregion Fields
#region Attached properties
/// <summary>
/// Gets or sets the type of the numeric value to be entered.
/// </summary>
public NumericTypes NumericType
{
get { return (NumericTypes)this.GetValue(NumericTypeProperty); }
set { this.SetValue(NumericTypeProperty, value); }
}
/// <summary>
/// Gets or sets the precision value.
/// </summary>
public int PrecisionValue
{
get { return (int)this.GetValue(PrecisionValueProperty); }
set { this.SetValue(PrecisionValueProperty, value); }
}
/// <summary>
/// The numeric type attached property
/// </summary>
public static readonly DependencyProperty NumericTypeProperty = DependencyProperty.Register("NumericType", typeof(NumericTypes), typeof(TextBoxNumeric), new PropertyMetadata(NumericTypes.Double));
/// <summary>
/// The precision attached property
/// </summary>
public static readonly DependencyProperty PrecisionValueProperty = DependencyProperty.Register("PrecisionValue", typeof(int), typeof(TextBoxNumeric), new PropertyMetadata(2));
#endregion Attached properties
#region Constructor/destructor
/// <summary>
/// Initializes the <see cref="TextBoxNumeric"/> class.
/// </summary>
static TextBoxNumeric()
{
}
/// <summary>
/// Finalizes an instance of the <see cref="TextBoxNumeric"/> class.
/// </summary>
~TextBoxNumeric()
{
if (this.eventHooked)
{
// unhook the preview event
this.PreviewTextInput += TextBoxNumeric_PreviewTextInput;
}
}
#endregion Constructor/destructor
/// <summary>
/// Is called when a control template is applied.
/// </summary>
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
if (!this.eventHooked)
{
this.eventHooked = true;
this.PreviewTextInput += TextBoxNumeric_PreviewTextInput;
}
}
/// <summary>
/// Handles the PreviewTextInput event of the TextBoxNumeric control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="TextCompositionEventArgs"/> instance containing the event data.</param>
private void TextBoxNumeric_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
switch (this.NumericType)
{
case NumericTypes.Integer : this.PreviewAsInteger(ref e); break;
case NumericTypes.Double : this.PreviewAsDouble(ref e); break;
case NumericTypes.Decimal : this.PreviewAsDecimal(ref e); break;
}
}
/// <summary>
/// Previews as integer.
/// </summary>
/// <param name="e">The <see cref="TextCompositionEventArgs"/> instance containing the event data.</param>
private void PreviewAsInteger(ref TextCompositionEventArgs e)
{
string text = e.Text;
TextBox textbox = (TextBox)(e.Source);
if (!text.IsNumeric())
{
e.Handled = true;
}
if (!e.Handled)
{
//this.GetTextBoxText(textbox, ref text);
}
}
/// <summary>
/// Previews as double.
/// </summary>
/// <param name="e">The <see cref="TextCompositionEventArgs"/> instance containing the event data.</param>
private void PreviewAsDouble(ref TextCompositionEventArgs e)
{
string text = e.Text;
TextBox textbox = (TextBox)(e.Source);
bool hasDecimal = textbox.Text.IndexOf(".") >= 0;
if (text == ".")
{
if (hasDecimal)
{
e.Handled = true;
}
else
{
hasDecimal = true;
}
}
else if (text != "." && !text.IsNumeric())
{
e.Handled = true;
}
if (!e.Handled)
{
this.GetTextBoxText(textbox, ref text);
int decLength = text.Length - ((hasDecimal) ? text.IndexOf(".") + 1 : 0);
if (hasDecimal && decLength > this.PrecisionValue)
{
e.Handled = true;
}
}
}
/// <summary>
/// Previews as decimal.
/// </summary>
/// <param name="e">The <see cref="TextCompositionEventArgs"/> instance containing the event data.</param>
private void PreviewAsDecimal(ref TextCompositionEventArgs e)
{
string text = e.Text;
TextBox textbox = (TextBox)(e.Source);
bool hasDecimal = textbox.Text.IndexOf(".") >= 0;
if (text == "." && hasDecimal)
{
e.Handled = true;
}
else if (text != "." && !text.IsNumeric())
{
e.Handled = true;
}
if (!e.Handled)
{
this.GetTextBoxText(textbox, ref text);
int decLength = text.Length - ((hasDecimal) ? text.IndexOf(".") + 1 : 0);
if (hasDecimal && decLength > this.PrecisionValue)
{
e.Handled = true;
}
}
}
/// <summary>
/// Gets the text box text for the preview methods.
/// </summary>
/// <param name="textbox">The textbox.</param>
/// <param name="text">The text.</param>
private void GetTextBoxText(TextBox textbox, ref string text)
{
int pos = textbox.SelectionStart;
if (pos == 0)
{
text = string.Format("{0}{1}", text, textbox.Text);
}
else if (pos == textbox.Text.Length)
{
text = string.Format("{0}{1}", textbox.Text, text);
}
else
{
List<char> chars = new List<char>();
chars.AddRange(textbox.Text.ToArray());
chars.InsertRange(pos, text.ToArray());
text = string.Empty;
foreach(char c in chars)
{
text += c;
}
}
}
/// <summary>
/// <para>Gets the numeric value respresented by the text. If you need to assign the return value to something other</para>
/// <para>than an int, double or decimal, you'll have to cast the return value to the desired type. Optionally, you </para>
/// <para>can add appropriate code to this class to add support for the desired numeric type(s).</para>
/// <para> </para>
/// <para>Usage is:</para>
/// <para> double value = textbox.GetNumericValue<double>();</para>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="ignoreException">if set to <c>true</c> this method returns 0 instead of throwing an exception if non-numeric text is encountered.</param>
/// <returns>
/// The Text property value as the specified type
/// </returns>
public T GetNumericValue<T>(bool ignoreException=true)
{
//return (T)Convert.ChangeType(this.Text, typeof(T));
return this.GetNumericValue((T)Convert.ChangeType(0, typeof(T)), ignoreException);
}
/// <summary>
/// <para>Gets the numeric value represented by the text.If you need to assign the return value to something other</para>
/// <para>than an int, double or decimal, you'll have to cast the return value to the desired type. Optionally, you </para>
/// <para>can add appropriate code to this class to add support for the desired numeric type(s).</para>
/// <para> </para>
/// <para>Usage is:</para>
/// <para> double value = textbox.GetNumericValue<double>();</para>
/// <para> </para>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="emptyValue">The value to return if the textbox is empty.</param>
/// <param name="ignoreException">if set to <c>true</c> this method returns the emptyValue instead of throwing an exception if non-numeric text is encountered.</param>
/// <returns>
/// The Text property value as the specified type
/// </returns>
/// <exception cref="System.Exception">TextBoxNumeric contains a non-numeric value.</exception>
public T GetNumericValue<T>(T emptyValue, bool ignoreException=true)
{
T result = emptyValue;
string text = this.Text;
if (!string.IsNullOrEmpty(text))
{
try
{
result = (T)Convert.ChangeType(text, typeof(T));
}
catch (Exception ex)
{
if (ignoreException)
{
result = emptyValue;
}
else
{
throw new Exception("TextBoxNumeric contains a non-numeric value.", ex);
}
}
}
return result;
}
}
}
这篇关于我将如何在文本框样式模板WPF winform中设置minlength属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!