Friday, April 1, 2011

Systematically Update Binding in Silverlight

So, after quite a bit of time in Silverlight, I have learned that DataForms are very powerful entities.  However, they like to hide from me what they are doing.  Therefore, when I get into a situation in which I need to simulate their behavior, this often ends with me banging my head against a wall and typing my query into Google in as many different ways as possible.  Recently, I have been attempting to force a validation on an object that was not in a DataForm... and I had no idea how.  Fortunately, I stumbled upon this forum and it helped me.  So that you don't have to bother reading it (that's why you're at my site, anyway, isn't it?) I have placed the important code snippet here:

BindingExpression bindingExpression = myTextBox.GetBindingExpresion(TextBox.TextProperty);
bindingExpression.UpdateSource();

Poof!  Your TextBox (assuming your TextBox variable is called "myTextBox") will validate.