Month: January 2012

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);

2011 in review

The WordPress.com stats helper monkeys prepared a 2011 annual report for this blog.

Here’s an excerpt:

The concert hall at the Syndey Opera House holds 2,700 people. This blog was viewed about 9,000 times in 2011. If it were a concert at Sydney Opera House, it would take about 3 sold-out performances for that many people to see it.

Click here to see the complete report.