我使用UpdateSourceTrigger.PropertyChanged将文本框绑定到整数。TextBox绑定:只从源更新一次
这似乎做工非常精细,除非我想改变让说1000至2000年 已经删除了1后,绑定是有点太聪明,截断文成一个0
我现在的解决方法是在经过一些过滤之后显式调用UpdateSource(),但它感觉不对。
是否有正确的方法来解决问题?
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
if (IsLoaded)
{
string text = ((TextBox)sender).Text;
if (text.Length <= 1 || !text.StartsWith("0"))
{
((TextBox)sender).GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
}
}
private void OnTestLostFocus(object sender, RoutedEventArgs e)
{
((TextBox)sender).GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
为什么不把'UpdateSourceTrigger'属性留作'Default'或'LostFocus'? – Dan
因为我想更新验证并在输入值时进行脏检查。在这种情况下是合理的,相信我。 – Karsten