SortDescriptor Class
[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.]
Describes a sorting criterion.
Namespace: System.Windows.Controls
Assembly: System.Windows.Controls.DomainServices (in System.Windows.Controls.DomainServices.dll)
The SortDescriptor type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | SortDescriptor | Initializes a new instance of the SortDescriptor class with default property values. |
![]() | SortDescriptor(String, ListSortDirection) | Initializes a new instance of the SortDescriptor class with the specified property to use for sorting, and the sort direction. |
| Name | Description | |
|---|---|---|
![]() | Direction | Gets or sets the sort direction. |
![]() | Dispatcher | (Inherited from DependencyObject.) |
![]() | PropertyPath | Gets or sets the public property used to sort. |
| Name | Description | |
|---|---|---|
![]() | CheckAccess | (Inherited from DependencyObject.) |
![]() | ClearValue | (Inherited from DependencyObject.) |
![]() | Equals | (Inherited from Object.) |
![]() | Finalize | (Inherited from Object.) |
![]() | GetAnimationBaseValue | (Inherited from DependencyObject.) |
![]() | GetHashCode | (Inherited from Object.) |
![]() | GetType | (Inherited from Object.) |
![]() | GetValue | (Inherited from DependencyObject.) |
![]() | MemberwiseClone | (Inherited from Object.) |
![]() | ReadLocalValue | (Inherited from DependencyObject.) |
![]() | SetValue | (Inherited from DependencyObject.) |
![]() | ToString | (Inherited from Object.) |
| Name | Description | |
|---|---|---|
![]() ![]() | DirectionProperty | Identifies the Direction dependency property. |
![]() ![]() | PropertyPathProperty | Identifies the PropertyPath dependency property. |
The DomainDataSource class provides the SortDescriptors collection to facilitate sorting data. In the SortDescriptors collection, you add SortDescriptor instances that describe the values to use for sorting the collection. You can add as many SortDescriptor instances as you want to provide layers of sorting. You can specify if the data is sorted in ascending or descending order.
If you use SortDescriptor programmatically, verify that the CanLoad property returns true. Attempting to sort when CanLoad returns false, causes the DomainDataSource to throw an invalid operation exception. Sorting initiates a load operation, and load operations are not permitted when CanLoad is false.
The following example shows how to add a sort descriptor to the DomainDataSource. The data retrieved from the query is sorted by values in the StandardCost property.
<Grid x:Name="LayoutRoot" Background="White"> <riaControls:DomainDataSource x:Name="source" QueryName="GetProducts" AutoLoad="true"> <riaControls:DomainDataSource.DomainContext> <domain:ProductDomainContext /> </riaControls:DomainDataSource.DomainContext> <riaControls:DomainDataSource.SortDescriptors> <riaData:SortDescriptor PropertyPath="StandardCost" Direction="Ascending" /> <riaData:SortDescriptor PropertyPath="ProductID" Direction="Ascending" /> </riaControls:DomainDataSource.SortDescriptors> </riaControls:DomainDataSource> <data:DataGrid ItemsSource="{Binding Data, ElementName=source}" /> </Grid>
When you implement paging and sorting together, include at least one SortDescriptor with its PropertyPath attribute assigned to a property that contains unique values, such as a primary key. Or add an OrderBy clause based on a property that contains unique values to the query in the DomainDataSource. If you only sort the data on a property that does not contain unique values, the return values could contain inconsistent or missing data across pages.
For example, consider the values in the following table. Note that the ID values are unique, but the Country values are not unique.
ID | Country |
|---|---|
1 | UK |
2 | UK |
3 | US |
4 | UK |
5 | US |
6 | IT |
7 | UK |
8 | UK |
9 | US |
10 | SP |
If you want to implement paging for these values, sorted by Country, you could use the following markup:
<Grid x:Name="LayoutRoot"> <ScrollViewer x:Name="PageScrollViewer" Style="{StaticResource PageScrollViewerStyle}"> <StackPanel x:Name="ContentStackPanel"> <TextBlock x:Name="HeaderText" Style="{StaticResource HeaderTextStyle}" Text="Home"/> <TextBlock x:Name="ContentText" Style="{StaticResource ContentTextStyle}" Text="Home page content"/> <riaControls:DomainDataSource Name="domainDataSource1" QueryName="GetCountriesQuery" PageSize="4"> <riaControls:DomainDataSource.DomainContext> <ds:TestDomainContext></ds:TestDomainContext> </riaControls:DomainDataSource.DomainContext> <riaControls:DomainDataSource.SortDescriptors> <riaControls:SortDescriptor PropertyPath="Country" Direction="Ascending"></riaControls:SortDescriptor> <riaControls:SortDescriptor PropertyPath="ID"></riaControls:SortDescriptor> </riaControls:DomainDataSource.SortDescriptors> </riaControls:DomainDataSource> <my:DataGrid ItemsSource="{Binding Data, ElementName=domainDataSource1}" /> <my:DataPager PageSize="4" Source="{Binding Data, ElementName=domainDataSource1}" /> </StackPanel> </ScrollViewer> </Grid>




