SL/RIA/POCO: Field Ordering
Posted
Mon, Jul 19 2010 15:30
by
Deborah Kurata
The prior post here detailed how to build a Silverlight application using WCF RIA Services and your own plain old CLR objects (POCOs). That application displayed the data in a grid and allowed updates using a set of labels and textboxes. But the order of the fields was alphabetical, which is rarely the order that you want.
So you could spend some time in the XAML, reordering the controls.
OR, you could add a few more attributes to your business object in your business layer and the fields will be ordered as you want.
Use the DisplayAttribute to:
- Define a field name different from the business object property name.
- Define a short name for use in the grid.
- Define a field to use as a watermark.
- Define the order of the field when building the UI.
- Define the resource type if the strings are defined in a resource file.
First, be sure to import the System.ComponentModel.DataAnnotations namespace. Then add the DisplayAttribute to the properties as shown in this example.
In C#:
[Display(Order=1, Description="Customer's last name", ShortName="LName", Name="Name (Last)")]
public string LastName { get; set; }
In VB:
<Display(Order:=1, Description:="Customer's last name", ShortName:="LName", Name:="Name (Last)")>
Public Property LastName() As String
The result looks like this:
Notice the use of the ShortName in the grid column header and of the Name in the field label. But more importantly for this column, notice the order of the fields both in the grid and in the data entry form.
Use this technique to define the field ordering before you drag and drop a grid or data entry form onto your Silverlight page.
Enjoy!