UpdateSource: Extension Method


You use UpdateSource on binding expressions to manually update the source. You usually do this to differ default update behavior. For example, if you have a TextBox and you have bound TextBox.Text property with your data object’s property, by default the source will update when the TextBox looses focus. In some cases you might want to alter this behavior. For example you want to update the source only when user hits “Save” button. You can achieve this by changing the BindingExpression’s UpdateSourceTrigger property to “Explicit”, and calling UpdateSource on the same BindingExpression from “Save” buttons click EventHandler. For complete code for a similar example head to MSDN

UpdateSource – The Extension Method

I shifted UpdateSource related code to an extension method for FrameworkElement to make it easier to call UpdateSource of the property directly from the FrameworkElement. You can utilize this method on all FrameworkElements throughout the project. So, in the example above, in EventHandler for button’s click, you could just call UpdateSource on the TextBox like this:


UpdateSource – The Extension

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace MyCompany.Extensions
    public static class FrameworkElementExtensions
        public static void UpdateSource(this FrameworkElement frameworkElement, DependencyProperty dp)
            BindingExpression expression = frameworkElement.GetBindingExpression(dp);
            if (expression != null)

One thought on “UpdateSource: Extension Method

  1. Pingback: C# | .NET Extension Methods | Sharp Statements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s