Geeks With Blogs
Braulio Díez Botella blog
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();
                    }
                }
        }
 
    }
Posted on Friday, October 2, 2009 8:13 AM | Back to top


Comments on this post: Textbox update binding on text changing

# re: Textbox update binding on text changing
Requesting Gravatar...
I found this issue and encapsulate it in an Attached Property to reuse this behaviour. I noticed I needed that most of the time, so I set that AP in the style.
Left by Miguel Madero on Oct 16, 2009 1:47 PM

Your comment:
 (will show your gravatar)


Copyright © Braulio Díez Botella | Powered by: GeeksWithBlogs.net