posts - 32, comments - 52, trackbacks - 0

My Links

News

Archives

Image Galleries

About Me

Friday, October 02, 2009

Textbox update binding on text changing

When using binding and textboxes you can find a nasty surprise… binding entities get only updated when your textbox looses the focus… this can work for you in a lot of scenarios, but in some other not, e.g. What happens if you want to enable a Save button based on some fields being informed ?, or you want to implement an autosave functionality?… if you want to make it based on the standard behavior the user would have to tab over the textbox and things will get updated…
Is there any workaround available? Yes, on this thread (http://forums.silverlight.net/forums/p/103695/294102.aspx#294102) found and interesting one (update the binding on the TextBoxchange event), but implementing this in every page could be a pain in the neck, so I have decided to create an extended version of TextBox control that includes this behavior by default:
    public class TextBoxChangeExt : System.Windows.Controls.TextBox
    {
        public TextBoxChangeExt()
        {
            this.TextChanged += new TextChangedEventHandler(TextBoxChangeExt_TextChanged);
        }
 
        void TextBoxChangeExt_TextChanged(object sender, TextChangedEventArgs e)
        {
                // based on
                // http://betaforums.silverlight.net/forums/t/103695.aspx
                TextBox txCtl = (TextBox)sender;
 
                if (txCtl != null)
                {
                    var be = txCtl.GetBindingExpression(TextBox.TextProperty);
                    if (be != null)
                    {
                        be.UpdateSource();
                    }
                }
        }
 
    }
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Posted On Friday, October 02, 2009 8:13 AM | Feedback (1) |

Powered by: