1

I have a TextBox with a two-way binding on the input. It is setup such that it fails validation if it is empty and displays a tooltip saying that it cannot be empty. My problem is that because it is failing validation, it tries to update the bindings everytime the text box changes (i.e. with every key press). I do not want it to update the source with every key press. I've narrowed it down to this code in the Silverlight 4.0 Tool kit for DataField.cs:

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);
        }
}

It is falling into the ValidationUtil.UpdateSourceOnElementBindings() because the element has errors. Is there anyway I can prevent it from doing this?

user1211135
  • 13
  • 1
  • 5

1 Answers1

0

I think you want help rearranging your conditions to more accurately express your intent, but I'm not clear on what the existing code's result would be. This is why we ask for a complete, runnable (but minimal!) test case. However, if you simply don't want to update due to a failed validation, this should do the trick:

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

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

You can validate the input and react to the result outside of that if statement.

Grault
  • 974
  • 12
  • 31