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.

Inheritance Hierarchy

System.Object
  System.Windows.DependencyObject
    System.Windows.Controls.SortDescriptor

Namespace:  System.Windows.Controls
Assembly:  System.Windows.Controls.DomainServices (in System.Windows.Controls.DomainServices.dll)

Syntax

'Declaration
Public Class SortDescriptor _
    Inherits DependencyObject
'Usage
Dim instance As SortDescriptor
public class SortDescriptor : DependencyObject
public ref class SortDescriptor : public DependencyObject
type SortDescriptor =  
    class
        inherit DependencyObject
    end
public class SortDescriptor extends DependencyObject

The SortDescriptor type exposes the following members.

Constructors

  Name Description
Public method SortDescriptor() Initializes a new instance of the SortDescriptor class with default property values.
Public method SortDescriptor(String, ListSortDirection) Initializes a new instance of the SortDescriptor class with the specified property to use for sorting, and the sort direction.

Top

Properties

  Name Description
Public property Direction Gets or sets the sort direction.
Public property Dispatcher (Inherited from DependencyObject.)
Public property PropertyPath Gets or sets the public property used to sort.

Top

Methods

  Name Description
Public method CheckAccess (Inherited from DependencyObject.)
Public method ClearValue (Inherited from DependencyObject.)
Public method Equals (Inherited from Object.)
Protected method Finalize (Inherited from Object.)
Public method GetAnimationBaseValue (Inherited from DependencyObject.)
Public method GetHashCode (Inherited from Object.)
Public method GetType (Inherited from Object.)
Public method GetValue (Inherited from DependencyObject.)
Protected method MemberwiseClone (Inherited from Object.)
Public method ReadLocalValue (Inherited from DependencyObject.)
Public method SetValue (Inherited from DependencyObject.)
Public method ToString (Inherited from Object.)

Top

Fields

  Name Description
Public fieldStatic member DirectionProperty Identifies the Direction dependency property.
Public fieldStatic member PropertyPathProperty Identifies the PropertyPath dependency property.

Top

Remarks

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.

Examples

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>

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

System.Windows.Controls Namespace