0

简而言之,我的问题:

我有一个 WPF 文本框,其中只允许用户输入十进制数字。我的 TextBox 的 PreviewTextInput 事件处理程序中有我的验证逻辑。

所以我试过了:

使用 TryParse

double number = 0;
bool isSuccessful = double.TryParse(e.Text, out number);
e.Handled = !(number >= 0 && isSuccessful);

使用正则表达式:(根据贾斯汀摩根对这个问题的回答

string validNumberFormat = @"^[-+]?(\d{1,3}((,\d{3})*(\.\d+)?|([.\s]\d{3})*(,\d+)?)|\d+([,\.]\d+)?)$";
e.Handled  = Regex.Matches(e.Text, validNumberFormat).Count < 1;

以上两种方法都只允许我输入数字,但不能输入单个小数点。

任何建议都将受到欢迎。

4

2 回答 2

2

是使用行为(Blend SDK System.Windows.Interactivity)而不是直接处理 PreviewTextInput。我这样做是因为可重用性,而且您应该知道 TextInput 没有处理空间,也没有处理粘贴。顺便说一句,关于您的“小数点”问题,我认为 Jonathan 和 Felice Pollano 是正确的。

public class TextBoxInputBehavior : Behavior<TextBox>
{
    public TextBoxInputMode InputMode { get; set; }

    public TextBoxInputBehavior()
    {
        this.InputMode = TextBoxInputMode.None;
    }

    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 input.CheckIsDigit();

            case TextBoxInputMode.DecimalInput:
                //minus einmal am anfang zulässig
                if (input == "-")
                    return true;
                decimal d;
                return decimal.TryParse(input, out d);
            default: throw new ArgumentException("Unknown TextBoxInputMode");

        }
        return true;
    }
}

public enum TextBoxInputMode
{
    None,
    DecimalInput,
    DigitInput
}

使用

   <TextBox>
        <i:Interaction.Behaviors>
            <MyBehaviors:TextBoxInputBehavior InputMode="DecimalInput"/>
        </i:Interaction.Behaviors>
    </TextBox>                
于 2012-07-06T07:23:06.287 回答
0

您可以使用以下免费控件。这将使您的工作变得轻松简单 http://wpftoolkit.codeplex.com/wikipage?title=DecimalUpDown&referringTitle=Home

http://wpftoolkit.codeplex.com/wikipage?title=DoubleUpDown&referringTitle=Home

于 2012-07-06T09:46:23.187 回答