Click here to Skip to main content
Licence CPOL
First Posted 6 Jul 2010
Views 8,404
Downloads 112
Bookmarked 9 times

Updating Source on PropertyChanged on Silverlight

By | 6 Jul 2010 | Article
A simple dependency property that allows you to update a property on PropertyChanged.

Introduction

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.

Background

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 =
    DependencyProperty.RegisterAttached(
        "UpdateOnPropertyChanged",
        typeof(bool),
        typeof(UpdateSourceManager),
        new PropertyMetadata(
            new PropertyChangedCallback(
                UpdateOnPropertyChangedPropertyCallback)));

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)
    {
        bindingExpression.UpdateSource();
    }
}

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

<UserControl x:class="UpdateSourceTrigger.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:UpdateSourceTrigger" 
    mc:ignorable="d">

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

<TextBox 
    Text="{Binding Context.FirstName, Mode=TwoWay}" 
    Grid.Column="1" 
    Grid.Row="0" 
    Margin="5" 
    local:UpdateSourceManager.UpdateOnPropertyChanged="True"
    />

The End

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

License

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

Architect

Brazil Brazil

Member

Follow on Twitter Follow on Twitter
Sênior .NET Architect from Brazil.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
SuggestionCan add a notifier dynamically Pinmemberchprogmer22:52 16 Nov '11  
GeneralMy vote of 5 PinmemberDuke Carey2:29 18 May '11  
GeneralGreat! PinmemberMarcelo Ricardo de Oliveira6:57 12 Jul '10  
GeneralRe: Great! PinmemberRaul Mainardi Neto7:01 12 Jul '10  
GeneralCool PinmemberAlan Beasley22:16 8 Jul '10  
GeneralRe: Cool PinmemberRaul Mainardi Neto15:14 9 Jul '10  
GeneralCool, makes sense to me. SL really is limited PinmvpSacha Barber4:51 8 Jul '10  
GeneralRe: Cool, makes sense to me. SL really is limited PinmemberRaul Mainardi Neto5:01 8 Jul '10  
GeneralRe: Cool, makes sense to me. SL really is limited PinmemberAlan Beasley22:12 8 Jul '10  
GeneralRe: Cool, makes sense to me. SL really is limited PinmvpSacha Barber23:01 8 Jul '10  

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.

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