Click here to Skip to main content
15,895,799 members
Articles / Programming Languages / C#
Tip/Trick

Silverlight object based databinding for custom properties

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
3 Oct 2011CPOL1 min read 11.8K   4   1
A simple solution that allows you to databind custom properties on your WCF objects without the need for painful Converters or custom code-behind logic.

Introduction


This article allows you to databind calculated/computed properties without the need to worry about creating converters or custom value-changed code-behind that is hard to maintain.


Background


SilverlightDataBinding.png


In the example presented, I am using a simple Expense object that has a few fields: FirstName, LastName, Utility, CreditCard, Other. The idea is to use these fields in a way that changes to any of the fields updates any computed custom fields that we may have such as FullName or TotalExpenses.


There are multiple places where you can place your custom property declarations. Either server side in a .shared.cs file or client side by creating a partial class extension.


I picked server side to keep the business logic in one place but that is not the point I am trying to make.


The main idea behind this article is to allow object based data binding that we can achieve by adding some hooks on the client side.


Assuming that we added the following code to the server side ServerSideEvents.shared.cs file:


C#
namespace SilverlightDatabinding.Web.DataModel
{
    public partial class Expense
    { 
        public string Name
        {
            get
            {
               return ((FirstName != null ? FirstName : "") + " " + 
                       (LastName != null ? LastName : "")).Trim();

            }
        }

        public decimal TotalExpenses
        {
            get
            {
                 
            return  (Utility.HasValue ? Utility.Value : 0M) + (CreditCard.HasValue ? 
               CreditCard.Value : 0M) + (Other.HasValue ? Other.Value : 0M);
            }
        }
    }
}

We can now add another client side partial class externsion CustomPropertiesExtension.cs:


C#
namespace SilverlightDatabinding.Web.DataModel
{
    public partial class Expense
    { 
        partial void OnFirstNameChanged()
        { 
            this.RaisePropertyChanged("Name");
        }

        partial void OnLastNameChanged()
        {
            this.RaisePropertyChanged("Name");
        }

        partial void OnUtilityChanged()
        {
            this.RaisePropertyChanged("TotalExpenses");
        }

        partial void OnCreditCardChanged()
        {
            this.RaisePropertyChanged("TotalExpenses");
        }

        partial void OnOtherChanged()
        {
            this.RaisePropertyChanged("TotalExpenses");
        }   
    }
}

Now in XAML, we can bind to the custom property directly without worrying about whether it will get updated properly on the UI or not.


HTML
<textblock verticalalignment="Center" text="{Binding TotalExpenses}" width="200" 
  fontweight="Black" grid.column="1" grid.row="6" horizontalalignment="Left" margin="5,5,5,5" />

Hope you find this useful. My first attempt to share some of my experiences.

License

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


Written By
CEO Sairoop Technologies
United States United States
This member doesn't quite have enough reputation to be able to display their biography and homepage.

Comments and Discussions

 
GeneralReason for my vote of 4 Achei este artigo muito bom mesmo, p... Pin
ARIBERTO11-Oct-11 0:17
professionalARIBERTO11-Oct-11 0:17 

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

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