How to: Add Computed Properties on the Client
[ This document was written for WCF Services Version 1 Service Pack 2 and might not be up to date Please see Release Notes or Changelog for a list of changes since WCF RIA Services ]
You can add properties in the Open 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. 1.
    In the client project, add a class file.
  2. 2.
    Declare a partial class with the same name and namespace as the entity proxy class you want to modify.
  3. 3.
    Add a property that creates a new value based on one or more values in the entity proxy class.
  4. 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.
    1
    Imports OpenRiaServices.Client
    2
    ​
    3
    Namespace Web
    4
    Partial Public Class Employee
    5
    Inherits Entity
    6
    ​
    7
    ReadOnly Property TotalOffHours() As Integer
    8
    Get
    9
    Return Me.SickLeaveHours + Me.VacationHours
    10
    End Get
    11
    End Property
    12
    ​
    13
    Private Sub OnSickLeaveHoursChanged()
    14
    Me.RaisePropertyChanged("TotalOffHours")
    15
    End Sub
    16
    ​
    17
    Private Sub OnVacationHoursChanged()
    18
    Me.RaisePropertyChanged("TotalOffHours")
    19
    End Sub
    20
    End Class
    21
    End Namespace
    Copied!
    1
    using OpenRiaServices.Client;
    2
    ​
    3
    namespace RIAServicesExample.Web
    4
    {
    5
    public partial class Employee : Entity
    6
    {
    7
    public int TotalOffHours
    8
    {
    9
    get { return this.SickLeaveHours + this.VacationHours; }
    10
    }
    11
    partial void OnSickLeaveHoursChanged()
    12
    {
    13
    this.RaisePropertyChanged("TotalOffHours");
    14
    }
    15
    partial void OnVacationHoursChanged()
    16
    {
    17
    this.RaisePropertyChanged("TotalOffHours");
    18
    }
    19
    }
    20
    }
    Copied!
    You can data bind to this computed property with the following code.
    1
    <dataForm:DataField Label="Total Off Hours">
    2
    <TextBox Text="{Binding TotalOffHours, Mode=OneWay}" />
    3
    </dataForm:DataField>
    Copied!
Last modified 6mo ago