How to force a binding to update

It is really very simple, all that we need is the FrameworkElement and the DependencyProperty that we need refresh. I did some extensions method to update the target or the source of the binding:

public static class BindingExtensions

{

    public static void UpdateTarget(

        this FrameworkElement element,

        DependencyProperty property)

    {

        BindingExpression expression = element.GetBindingExpression(property);

        if (expression != null)

        {

            expression.UpdateTarget();

        }

    }

 

    public static void UpdateSource(

        this FrameworkElement element,

        DependencyProperty property)

    {

        BindingExpression expression = element.GetBindingExpression(property);

        if (expression != null)

        {

            expression.UpdateSource();

        }

    }

}

For example if we want to refresh the Text property in a bound TextBox we can do:

textBox.UpdateTarget(TextBox.TextProperty);

1 Comment

Comments have been disabled for this content.