我有一个TextBox,在输入上有双向绑定。设置为如果它为空则验证失败并显示工具提示,表明它不能为空。我的问题是,因为验证失败,它会在文本框每次更改时尝试更新绑定(即每按一次键)。我不希望它按每次按键更新源。我已经将它缩小到适用于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(),因为该元素有错误。无论如何我可以阻止它这样做吗?
答案 0 :(得分:0)
我认为您需要帮助重新安排条件以更准确地表达您的意图,但我不清楚现有代码的结果是什么。这就是为什么我们要求一个完整的,可运行的(但最小的!)测试用例。但是,如果由于验证失败而您根本不想更新,这应该可以解决问题:
private void OnTextBoxTextChanged(object sender, TextChangedEventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox != null && !this._lostFocusFired[textBox]))
{
ValidationUtil.UpdateSourceOnElementBindings(textBox);
}
}
您可以验证输入并对该if语句之外的结果作出反应。