因为我是WPF开发的新手。
我想知道如何处理数字文本框。
在旧的Windows开发应用程序中,我可以处理上述场景 在Key Press活动中但在WPF中我不会有这个活动。
所以我必须在Key Down Event中处理这种情况。
但它很复杂我能否知道如何处理这个问题。
条件:
应仅允许数字和单个小数点。
小数点后应仅允许2个字符(数字)。
答案 0 :(得分:0)
您必须通过继承本机TextBox来创建扩展文本框。并覆盖OnTextInput方法。根据需要处理输入。以下代码段仅允许数字而不允许字符。同样的方法可以验证您的其他需求。
protected override void OnTextInput(TextCompositionEventArgs e)
{
string text = e.Text.ToString();
double output = 0.0;
bool isnumber = Double.TryParse(text, out output);
if (!isnumber)
{
e.Handled = true;
}
base.OnTextInput(e);
}
答案 1 :(得分:0)
你可以使用这种东西的行为
public class TextBoxInputBehavior : Behavior<TextBox>
{
const NumberStyles validNumberStyles = NumberStyles.AllowDecimalPoint |
NumberStyles.AllowThousands |
NumberStyles.AllowLeadingSign;
public TextBoxInputBehavior()
{
this.InputMode = TextBoxInputMode.None;
}
public TextBoxInputMode InputMode { get; set; }
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.PreviewTextInput += AssociatedObjectPreviewTextInput;
AssociatedObject.PreviewKeyDown += AssociatedObjectPreviewKeyDown;
DataObject.AddPastingHandler(AssociatedObject, Pasting);
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.PreviewTextInput -= AssociatedObjectPreviewTextInput;
AssociatedObject.PreviewKeyDown -= AssociatedObjectPreviewKeyDown;
DataObject.RemovePastingHandler(AssociatedObject, Pasting);
}
private void Pasting(object sender, DataObjectPastingEventArgs e)
{
if (e.DataObject.GetDataPresent(typeof(string)))
{
var pastedText = (string)e.DataObject.GetData(typeof(string));
if (!this.IsValidInput(this.GetText(pastedText)))
{
System.Media.SystemSounds.Beep.Play();
e.CancelCommand();
}
}
else
{
System.Media.SystemSounds.Beep.Play();
e.CancelCommand();
}
}
private void AssociatedObjectPreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space)
{
if (!this.IsValidInput(this.GetText(" ")))
{
System.Media.SystemSounds.Beep.Play();
e.Handled = true;
}
}
}
private void AssociatedObjectPreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (!this.IsValidInput(this.GetText(e.Text)))
{
System.Media.SystemSounds.Beep.Play();
e.Handled = true;
}
}
private string GetText(string input)
{
var txt = this.AssociatedObject;
var realtext = txt.Text.Remove(txt.SelectionStart, txt.SelectionLength);
var newtext = realtext.Insert(txt.CaretIndex, input);
return newtext;
}
private bool IsValidInput(string input)
{
switch (InputMode)
{
case TextBoxInputMode.None:
return true;
case TextBoxInputMode.DigitInput:
return CheckIsDigit(input);
case TextBoxInputMode.DecimalInput:
//minus einmal am anfang zulässig
if (input.Contains("-"))
if (input.IndexOf("-") == 0 && input.LastIndexOf("-")==0)
return true;
else
return false;
//wen mehr als ein Komma
if (input.ToCharArray().Where(x => x == ',').Count() > 1)
return false;
decimal d;
return decimal.TryParse(input,validNumberStyles,CultureInfo.CurrentCulture, out d);
default: throw new ArgumentException("Unknown TextBoxInputMode");
}
return true;
}
private bool CheckIsDigit(string wert)
{
return wert.ToCharArray().All(Char.IsDigit);
}
}
public enum TextBoxInputMode
{
None,
DecimalInput,
DigitInput
}
XAML
<TextBox Text="{Binding MyDecimalProperty}">
<i:Interaction.Behaviors>
<Behaviors:TextBoxInputBehavior InputMode="DecimalInput"/>
</i:Interaction.Behaviors>
</TextBox>
答案 2 :(得分:-1)
//It's not satisfy second condition
TextBox txtDecimal = sender as TextBox;
if ((e.Key == Key.OemPeriod || e.Key == Key.Decimal) && (txtDecimal.Text.Contains(".") == false))
{ e.Handled = false;}
else if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || (e.Key == Key.Back))
{ e.Handled = false; }
else if((e.Key >= Key.D0 && e.Key <= Key.D9))
{ e.Handled = false; }
else
{ e.Handled = true; }