Click here to Skip to main content
11,409,450 members (48,360 online)
Click here to Skip to main content

Updating Source on PropertyChanged on Silverlight

, 6 Jul 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple dependency property that allows you to update a property on PropertyChanged.


Don't you miss the "UpdateSourceTrigger=PropertyChanged" on a Binding expression on Silverlight? Well, me too, and since I don't like the idea of having a hidden control to change focus on every TextChange of my TextBoxes, I tried to make a more elegant solution for the problem.


In both WPF and Silverlight, when we create a BindingExpression, you can define a UpdateSourceTrigger property of the Binding. But, there is a huge difference between them, Silverlight has only the Default mode (LostFocus) and Explicit, so we are missing PropertyChanged that in some cases makes our life way easier.

Using the Code

The code is fairly simple. It is only one AttachedProperty that will handle the TextChanged for you and will update the BindingSource if the BindingExpression of the TextProperty exists.

This was only implemented in the TextBox because there is no need to change the other input controls over the fact that LostFocus works perfect for them (like ComboBoxes, CheckBoxes, RadioButtons).

So, we define an AttachedProperty that shouldn't be a problem to any Silverlight developer:

public static bool GetUpdateOnPropertyChanged(DependencyObject obj)
    return (bool)obj.GetValue(UpdateOnPropertyChangedProperty);

public static void SetUpdateOnPropertyChanged(DependencyObject obj, bool value)
    obj.SetValue(UpdateOnPropertyChangedProperty, value);

public static readonly DependencyProperty UpdateOnPropertyChangedProperty =
        new PropertyMetadata(
            new PropertyChangedCallback(

Then we define the UpdateOnPropertyChangedPropertyCallback:

static void UpdateOnPropertyChangedPropertyCallback(
    DependencyObject sender, DependencyPropertyChangedEventArgs e)
    if (sender is TextBox)
        TextBox txt = sender as TextBox;
        txt.TextChanged += new TextChangedEventHandler(txt_TextChanged);

static void txt_TextChanged(object sender, TextChangedEventArgs e)
    TextBox txt = sender as TextBox;

    var bindingExpression = txt.GetBindingExpression(TextBox.TextProperty);
    if (bindingExpression != null)

Then, for using this, just add to your UserControl the proper XML namespace like:

<UserControl x:class="UpdateSourceTrigger.MainPage"

And finally, add the Attached Property to the TextBox as follows:

    Text="{Binding Context.FirstName, Mode=TwoWay}" 

The End

Hope this can make things easier to you, as it did to me.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Raul Mainardi Neto
Brazil Brazil
Senior .NET Architect from Brazil.
Follow on   Twitter

Comments and Discussions

QuestionUpdateSourceManager weird bug on a deployed View PinmemberMycroft Holmes28-Aug-12 15:54 
If you use your Helper:UpdateSourceManager.UpdateOnPropertyChanged="True" on a textbox with a StringFormat StringFormat= '#.00000000' or {0:n8} there is an issue when entering numbers with a period. The cursor jumps back to the front of the period so typing 32.97 looks like 2.973.00000000

This does not have a problem in the development environment but only on a DEPLOYED view. Removing either the stringformat or the UpdateSourceManager fixes the problem.

This thing makes the UI so much more usable that I would like to use it on numerics as well.
Never underestimate the power of human stupidity

GeneralMy vote of 4 PinmemberAlbert van Peppen27-Jul-12 1:11 
GeneralMy vote of 5 PinmemberMycroft Holmes12-Jul-12 19:19 
SuggestionCan add a notifier dynamically Pinmemberchprogmer16-Nov-11 23:52 
GeneralMy vote of 5 PinmemberDuke Carey18-May-11 3:29 
GeneralGreat! PinmemberMarcelo Ricardo de Oliveira12-Jul-10 7:57 
GeneralRe: Great! PinmemberRaul Mainardi Neto12-Jul-10 8:01 
GeneralCool PinmemberAlan Beasley8-Jul-10 23:16 
GeneralRe: Cool PinmemberRaul Mainardi Neto9-Jul-10 16:14 
GeneralCool, makes sense to me. SL really is limited PinmvpSacha Barber8-Jul-10 5:51 
GeneralRe: Cool, makes sense to me. SL really is limited PinmemberRaul Mainardi Neto8-Jul-10 6:01 
GeneralRe: Cool, makes sense to me. SL really is limited PinmemberAlan Beasley8-Jul-10 23:12 
GeneralRe: Cool, makes sense to me. SL really is limited PinmvpSacha Barber9-Jul-10 0:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150414.5 | Last Updated 6 Jul 2010
Article Copyright 2010 by Raul Mainardi Neto
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid