Home > English > Sorting a DomainDataSource in Xaml

Sorting a DomainDataSource in Xaml

Sometimes when using the DomainDataSource control, it just doesn’t makes sense to write a custom query method only to provide sorting capabilities. Fortunately this is provided out of the box for you. You can do this either in XAML or you and programmatically code this up yourself.

The following is an example of doing a simple sort operation in Xaml:

<riaCtls:DomainDataSource
    x:Name="ddsApplications" QueryName="GetCg_xa_ApplicationQuery"
    DomainContext="{Binding DomainContext, Mode=TwoWay}">
    <riaCtls:DomainDataSource.SortDescriptors>
        <riaCtls:SortDescriptor PropertyPath="Name" Direction="Ascending" />
    </riaCtls:DomainDataSource.SortDescriptors>
</riaCtls:DomainDataSource>

The underlying grid is just setting its ItemsSource property to the Data property of the DomainDataSource. Here is the output of the sorted data in a grid:

Sorting Sample

A little more complex example is if you want to have a multi-column sort:

<riaCtls:DomainDataSource
    x:Name="ddsMasterForms" QueryName="ReturnMasterFormsIncludingForeignKeysQuery"
    DomainContext="{Binding DomainContext, Mode=TwoWay}">
    <riaCtls:DomainDataSource.SortDescriptors>
        <riaCtls:SortDescriptor PropertyPath="Tag.Name" Direction="Ascending" />
        <riaCtls:SortDescriptor PropertyPath="Name" Direction="Ascending" />
    </riaCtls:DomainDataSource.SortDescriptors>
</riaCtls:DomainDataSource>

As you can see, I wrote a custom query that will also bring back all of the foreign keys associated with my main object. I then insert the corresponding SortDescriptor objects in the order that I wish to perform the sorting operation. This is nice since I can go across objects or properties at the same level. The underlying grid is just setting its ItemsSource property to the Data property of the DomainDataSource.

Here is a screen shot of the complex sort:

Complex Sorting Sample

Finally, you can now using DataBinding if you wanted to have a more dynamic sorting.

<riaCtls:DomainDataSource
    x:Name="ddsApplications" QueryName="GetCg_xa_ApplicationQuery"
    DomainContext="{Binding DomainContext, Mode=TwoWay}">
    <riaCtls:DomainDataSource.SortDescriptors>
        <riaCtls:SortDescriptor
            PropertyPath="{Binding Path=SelectedItem.Content, ElementName=cboSort}"
            Direction="Ascending" />
    </riaCtls:DomainDataSource.SortDescriptors>
</riaCtls:DomainDataSource>

The above example assumes you have a ComboBox named, “cboSort”. If you want to bind to your ViewModel in MVVM:

<riaCtls:DomainDataSource
    x:Name="ddsApplications" QueryName="GetCg_xa_ApplicationQuery"
    DomainContext="{Binding DomainContext, Mode=TwoWay}">
    <riaCtls:DomainDataSource.SortDescriptors>
        <riaCtls:SortDescriptor
            PropertyPath="{Binding CurrentSortText}"
            Direction="Ascending" />
    </riaCtls:DomainDataSource.SortDescriptors>
</riaCtls:DomainDataSource>

The above example assumes you have a property called, “CurrentSortText” on your ViewModel. This gives you some flexibility of how you want to interact with the DomainDataSource.

NOTE: If you leave off the Direction property on the SortDescriptor, the default direction will be ascending.

Hope this helps…

Advertisements
  1. June 2, 2010 at 10:57 pm

    Suitably written.
    You have a nice blog.
    Thank you for posting this.

  2. September 7, 2010 at 9:04 pm

    Very Interesting!
    Thank You!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: