TextChanged事件的奇怪错误(WPF文本框)

时间:2012-08-11 16:04:06

标签: c# .net wpf

我有一个文本框,我试图以两种方式限制:

1 - 我只想允许数字值,没有小数

2 - 我只想接受< = 35

的数字

我有以下事件来处理这个问题:

private void TextBoxWorkflowCountPreviewTextInput(object sender, TextCompositionEventArgs e)
{
    if (!IsNumeric(e.Text, NumberStyles.Integer)) e.Handled = true;
}

public bool IsNumeric(string val, NumberStyles numberStyle)
{
    double result;
    return double.TryParse(val, numberStyle, CultureInfo.CurrentCulture, out result);
}

private void TextBoxWorkflowCountTextChanged(object sender, TextChangedEventArgs e)
{
    if (!string.IsNullOrEmpty(textBoxWorkflowCount.Text) && Convert.ToInt32(textBoxWorkflowCount.Text) <= 35) e.Handled = true;
    else
    {
        MessageBox.Show("Must not be higher then 35");
        textBoxWorkflowCount.Text = "35";
    }
}

表面上的效果非常好 - 除了,当用户将数据粘贴到文本框中时(看起来不可避免)或者更奇怪的是 - 如果用户输入数字然后点击退格(制作文本框再次空白)消息框让用户知道他们的值> 35(即使绝对不是这种情况)。如果必须的话,我可以忍受的第一个问题 - 但第二个问题是游戏破坏,在尝试解决它的30分钟后我无处可去。救命啊!

2 个答案:

答案 0 :(得分:1)

您的代码在第一个条件失败,因为

string.IsNullOrEmpty(textBoxWorkflowCount.Text) 

评估为true,因此它会落入'else'并显示消息。

if (string.IsNullOrEmpty(textBoxWorkflowCount.Text) || Convert.ToInt32(textBoxWorkflowCount.Text) <= 35) e.Handled = true; 

应该做的伎俩

答案 1 :(得分:1)

几个月前,我wrote a blog发布了关于TextBox行为(Windows Phone 7.5平台)的文章,其中一个行为是TextBoxInputRegexFilterBehavior,它允许您过滤输入文本。因此,如果您熟悉行为的工作方式,则可以使用此代码

using System.Text.RegularExpressions;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;

/// <summary>
/// UI behavior for <see cref="TextBox"/> to filter input text with special RegularExpression.
/// </summary>
public class TextBoxInputRegexFilterBehavior : Behavior<TextBox>
{
    private Regex regex;

    private string originalText;
    private int originalSelectionStart;
    private int originalSelectionLength;

    /// <summary>
    /// Gets or sets RegularExpression.
    /// </summary>
    public string RegularExpression 
    {
        get
        {
            return this.regex.ToString();
        } 

        set 
        {
            if (string.IsNullOrEmpty(value))
            {
                this.regex = null;
            }
            else
            {
                this.regex = new Regex(value);
            }
        } 
    }

    /// <inheritdoc/>
    protected override void OnAttached()
    {
        base.OnAttached();

        this.AssociatedObject.TextInputStart += this.AssociatedObjectTextInputStart;
        this.AssociatedObject.TextChanged += this.AssociatedObjectTextChanged;
    }

    /// <inheritdoc/>
    protected override void OnDetaching()
    {
        base.OnDetaching();

        this.AssociatedObject.TextInputStart -= this.AssociatedObjectTextInputStart;
        this.AssociatedObject.TextChanged -= this.AssociatedObjectTextChanged;
    }

    private void AssociatedObjectTextChanged(object sender, TextChangedEventArgs e)
    {
        if (this.originalText != null)
        {
            string text = this.originalText;
            this.originalText = null;
            this.AssociatedObject.Text = text;
            this.AssociatedObject.Select(this.originalSelectionStart, this.originalSelectionLength);
        }
    }

    private void AssociatedObjectTextInputStart(object sender, TextCompositionEventArgs e)
    {
        if (this.regex != null && e.Text != null && !(e.Text.Length == 1 && char.IsControl(e.Text[0])))
        {
            if (!this.regex.IsMatch(e.Text))
            {
                this.originalText = this.AssociatedObject.Text;
                this.originalSelectionStart = this.AssociatedObject.SelectionStart;
                this.originalSelectionLength = this.AssociatedObject.SelectionLength;
            }
        }
    }
}

所以,有了这种行为你可以编写简单的正则表达式来过滤用户输入,比如这个RegularExpression =“(3 [0-5])|([0-2]?[0-9])”