lunes, julio 28, 2008

How to Update a TextBox Binding OnPropertyChanged

When using TwoWay Binding, most properties get updated after the control's property changes, but the Text in a TextBlock is a different case, it gets updated after the TextBox lost focus. It makes sense, since the value might be incomplete and validations and other logic triggered at the Business Object level unnecessarily, but sometimes this is not the desired behavior.

WPF has an UpdateSourceTrigger as part of the Binding object and you could say something like:

      <TextBox Text={Binding Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}>

This is not available in Silverlight, but there's a quick workaround provided by Yi-Lun Luo in this thread.

        private void cantidadTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            otherControl.Focus();
            cantidadTextBox.Focus();
        }

It's really ugly, but works.

 

2 comentarios:

Anónimo dijo...

GetBindingExpression(TextBox.TextProperty).UpdateSource();

Miguel Madero dijo...

Thanks. My post was referring to SL2 where GetBindingExpression wasn't available.