Export (0) Print
Expand All
2 out of 6 rated this helpful - Rate this topic

How to: Add Computed Properties on the Client

WCF RIA Services

[WCF RIA Services Version 1 Service Pack 2 is compatible with either .NET framework 4 or .NET Framework 4.5, and with either Silverlight 4 or Silverlight 5.]

You can add properties in the WCF RIA Services client project that are computed from properties in the entity class. Partial methods are used to raise the event that notifies UI elements that the value has changed. When you add computed properties, the property only exists in the client project. This topic describes how to add such computed properties.

For more information about customizing generated code, see Customizing Generated Code. For information about generating client code, see Client Code Generation.

To add a computed property

  1. In the client project, add a class file.

  2. Declare a partial class with the same name and namespace as the entity proxy class you want to modify.

  3. Add a property that creates a new value based on one or more values in the entity proxy class.

  4. Implement the On[CustomProperty]Changed partial method for each property used in computing the new value and call the RaisePropertyChanged method to notify the framework that the computed property has changed.

    The following example shows how to calculate the total available time away from work for an employee based on vacation hours and sick leave. A change to either vacation hours or sick leave hours will produce a change in total off hours.

    Imports System.ServiceModel.DomainServices.Client
    
    Namespace Web
      Partial Public Class Employee
        Inherits Entity
    
        ReadOnly Property TotalOffHours() As Integer
          Get
            Return Me.SickLeaveHours + Me.VacationHours
          End Get
        End Property
    
        Private Sub OnSickLeaveHoursChanged()
          Me.RaisePropertyChanged("TotalOffHours")
        End Sub
    
        Private Sub OnVacationHoursChanged()
          Me.RaisePropertyChanged("TotalOffHours")
        End Sub
      End Class
    End Namespace
    

    using System.ServiceModel.DomainServices.Client;
    
    namespace RIAServicesExample.Web
    {
      public partial class Employee : Entity
      { 
        public int TotalOffHours 
        { 
          get { return this.SickLeaveHours + this.VacationHours; } 
        } 
        partial void OnSickLeaveHoursChanged() 
        { 
          this.RaisePropertyChanged("TotalOffHours"); 
        } 
        partial void OnVacationHoursChanged() 
        { 
          this.RaisePropertyChanged("TotalOffHours"); 
        } 
      }
    }
    

    You can data bind to this computed property with the following code.

    <dataForm:DataField Label="Total Off Hours"> 
      <TextBox Text="{Binding TotalOffHours, Mode=OneWay}" />
    </dataForm:DataField>
    
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.