Refresh a controls binding in Silverlight or WPF without using PropertyChanged

I often need to change the Language (or culture) of a control (changing the Control.Language) or the whole thread (changing CurrentThread.CurrentUICulture). Such as when displaying dates and currencies in a particular culture. Anyone who has tried this will realise that the controls will not reflect the change until an underlying value has updated.

Alternatively you can rebind the control. To help I have created a simple generic method to help.

        
    ///<summary>
    /// Rebinds a controls dependency property to it's original binding. Used to refresh a control.
    /// </summary>
    /// <remarks>Can be used after updating a controls Culture</remarks>
    /// <param name="cntrl">The control to which the dependency property exists</param>
    /// <param name="depprop">The dependency property of the control</param>
    public static void ForceControlRebind(Control cntrl, DependencyProperty depprop)
        {
            try
            {
                BindingExpression BindExp = cntrl.GetBindingExpression(depprop);
                Binding Bind = BindExp.ParentBinding;
                cntrl.SetBinding(depprop, Bind);
            }
            catch (Exception)
            {
                throw;
            }
        }

To rebind a control just pass the Control and the DependencyProperty whose binding you wish to refresh.

So, for a TextBox called txtBox1 the syntax would be:

ForceControlRebind(txtBox1, TextBox.TextProperty);

Leave a Reply