Here it is: The UpdateSourceTrigger for PropertyChanged in Silverlight

Today I’ll show you how to implement a PropertyChanged-UpdateSourceTrigger for Silverlight. In Silverlight a Data Binding can have different UpdateSourceTriggers. An UpdateSourceTrigger specifies, when a TwoWay-DataBinding will update its source with the value of the Target-Property.

In most cases you have a TwoWay-DataBinding on TextBoxes. When the user types something into a TextBox, it should be written back to the underlying Data-Object. The other way, when the underlying Data-Object changes, the TextBox should show the actual value of the bound property.

In this post I’ll focus on the moment of writing the data back to the underlying object. In other words, when the data is updated with the text the user has typed in.

In Silverlight the underlying object is updated after the TextBox has lost its focus. In WPF, the UpdateSourceTrigger-Enum has a Member PropertyChanged. If you set the UpdateSourceTrigger-Property of the Data Binding to that value, the underlying object will be updated everytime the user changes the text. Unfortunately Silverlight doesn’t contain the PropertyChanged-Value in its UpdateSourceTrigger-Enum. It only contains a Default- and a LostFocus-Member. As the name implies, LostFocus will update the source when the TextBox loses its focus. But an Attached Property would do the trick.

When I tried to solve this problem I came across a post of Michael Sync, who showed up a solution with an attached-Property that works by stealing the focus everytime the Text of a TextBox changes, and immediately set it back to the TextBox again. Find his post under

As I got deep knowledge about WPFs Binding-Engine, I could produce a solution for Silverlight, and I think I got a similar solution to Michael’s, but my solution is built closer on the binding-engine and doesn’t need a focus-trick. So, some details: Behind each Binding, a BindingExpression does the real work. All you have to do when the Text of the TextBox changes is to get the BindingExpression of the Data Binding and to call its UpdateSource-Method. So my Attached-Property-Class looks like the one below (Feel free to use the source in your projects, but please keep the reference to As it is, it just works for the Text-Property of TextBoxes.

The usage of the BindingHelper-class is very simple. Just add it to your project, insert a matching xmlns to your XAML file that contains the CLR-Namespace the BindingHelper-class is in. When that is done, you can simply set the UpdateSourceOnChange-Property on a TextBox to true, and if the Text-Property is databound, you’ll have a Data Binding that will update its source on every PropertyChange. Like in WPF. :-)

If this post helped you, please kick it. :-)

kick it on

12 thoughts on “Here it is: The UpdateSourceTrigger for PropertyChanged in Silverlight”

  1. Any chance of a VB.NET version?
    Tried converting, but Iam stuck on:

    txt.TextChanged += OnTextChanged;

    This is not allowed in VB.NET

  2. txt.TextChanged += OnTextChanged;
    addhandler txt.TextChanged,addressof OnTextChanged
    removehandler txt.TextChanged,addressof OnTextChanged

  3. For help in VB :
    Public Class BindingHelper
    Public Shared Function GetUpdateSourceOnChange(obj As DependencyObject) As Boolean
    Return obj.GetValue(UpdateSourceOnChangeProperty)
    End Function
    Public Shared Sub SetUpdateSourceOnChange(obj As DependencyObject, value As Boolean)
    obj.SetValue(UpdateSourceOnChangeProperty, value)
    End Sub
    Public Shared ReadOnly UpdateSourceOnChangeProperty As DependencyProperty =
    DependencyProperty.RegisterAttached(“UpdateSourceOnChange”, GetType(Boolean), GetType(BindingHelper), New PropertyMetadata(False, AddressOf OnPropertyChanged))

    Private Shared Sub OnPropertyChanged(obj As DependencyObject, e As DependencyPropertyChangedEventArgs)
    Dim TxtBox As TextBox = obj
    If TxtBox IsNot Nothing Then
    If e.NewValue Then
    AddHandler TxtBox.TextChanged, AddressOf OnTextChanged
    RemoveHandler TxtBox.TextChanged, AddressOf OnTextChanged
    End If
    End If
    End Sub
    Private Shared Sub OnTextChanged(sender As Object, e As TextChangedEventArgs)
    Dim TxtBox As TextBox = sender
    If TxtBox IsNot Nothing Then
    Dim be = TxtBox.GetBindingExpression(TextBox.TextProperty)
    If be IsNot Nothing Then
    End If
    End If
    End Sub
    End Class

  4. This is fixed in SL 5. The default value remains LostFocus, but the enumeration now contains PropertyChanged as well, wo working in SL 5 doesn’t require this workaround anymore.

Comments are closed.