views:

94

answers:

2

I have a databound Silverlight DataGrid control that I am trying to sort. I am using RIA services (beta) for my data source, if that makes any difference.

I am quite new to databinding in Silverlight, so this might be something really obvious that I've missed, but I can't seem to find any info on it. I want to be able to set the binding of the ItemSource to a collection in xaml using binding syntax, and have it sorted on one column.

I realize I could set the ItemsSource in code and use LINQ to .OrderBy(). But I don't get a binding that way. It seems like there should be a simple way to do this but I can't find one. How can I keep the binding yet order my collection?

+1  A: 

As you are using RIA Services, you can use the DomainDataSource in your XAML. This will allow you to add SortDescriptors which will do your ordering. See my example below:

<riaControls:DomainDataSource.SortDescriptors>
    <riaData:SortDescriptor Direction="Ascending" 
                            PropertyPath="Name" />
</riaControls:DomainDataSource.SortDescriptors>

Ardman
+1  A: 

Hi Dale, have a look at using a CollectionViewSource. You basically use one as a 'middleman' between your actual collection of data and you data-bound control.

rough example:

<Window.Resources>
    <CollectionViewSource 
              Source="{Binding <<<bind to your collection here >>> }"   
              x:Key="myDataView" />

    </Window.Resources>

...

<ListBox Name="lsyFoo" 
    ItemsSource="{Binding Source={StaticResource myDataView}}">

...

then in your code behind:

myDataView.SortDescriptions.Add(
                new SortDescription("<<<insert property to sort by>>>", ListSortDirection.Ascending));

(ps. you can also add grouping using PropertyGroupDescription)

Jason Roberts
Yea, this is exactly what I had to do. Thanks.
Dale Halliwell