【问题标题】:TextBox DataField always updating source bindings on text changed with text box that fails validationTextBox DataField 总是更新文本上的源绑定,该文本已通过验证失败的文本框更改
【发布时间】:2012-02-15 11:21:14
【问题描述】:

我有一个文本框,在输入端具有双向绑定。它被设置为如果它为空则验证失败并显示一个提示它不能为空的工具提示。我的问题是,因为它没有通过验证,所以每次文本框更改时(即每次按键)它都会尝试更新绑定。我不希望它在每次按键时都更新源。我已将其缩小到适用于 DataField.cs 的 Silverlight 4.0 工具包中的以下代码:

private void OnTextBoxTextChanged(object sender, TextChangedEventArgs e)
{
    TextBox textBox = sender as TextBox;

    if (textBox != null && (ValidationUtil.ElementHasErrors(textBox) || !this._lostFocusFired[textBox]))
        {
            this._lostFocusFired[textBox] = false;
            ValidationUtil.UpdateSourceOnElementBindings(textBox);
        }
}

它属于 ValidationUtil.UpdateSourceOnElementBindings() 因为元素有错误。无论如何我可以阻止它这样做吗?

【问题讨论】:

    标签: silverlight-4.0 silverlight-toolkit


    【解决方案1】:

    我认为您需要帮助重新安排条件以更准确地表达您的意图,但我不清楚现有代码的结果是什么。这就是为什么我们要求一个完整的、可运行的(但最小的!)测试用例。但是,如果您只是因为验证失败而不想更新,这应该可以解决问题:

    private void OnTextBoxTextChanged(object sender, TextChangedEventArgs e)
    {
        TextBox textBox = sender as TextBox;
    
        if (textBox != null && !this._lostFocusFired[textBox]))
        {
            ValidationUtil.UpdateSourceOnElementBindings(textBox);
        }
    }
    

    您可以在 if 语句之外验证输入并对结果做出反应。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-11
      • 1970-01-01
      • 2016-04-10
      • 2010-11-21
      • 1970-01-01
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多