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

Published Thursday, June 04, 2009 12:24 PM by marianor
Filed under:

Comments

# How to force a binding to update - Unknown Recipes

Thursday, June 04, 2009 10:55 AM by How to force a binding to update - Unknown Recipes

Pingback from  How to force a binding to update - Unknown Recipes

# How to force a binding to update | rapid-DEV.net

Sunday, June 14, 2009 12:38 PM by How to force a binding to update | rapid-DEV.net

Pingback from  How to force a binding to update | rapid-DEV.net

Leave a Comment

(required) 
(required) 
(optional)
(required)