Hello Folks,
Want to create Silverlight datagrid with sorting funtionality. You don't need to
write 100 lines of code to achieve this. Thisis because when you bound collection
that implements IList, Silverlight will internally create ListCollectionView to
provide sorting functionality. The only thing that you need to do is to set the
CanUserSort property of the column
to true. By default each column for which CanUserSort attribute is set to true is
sorted by the property which is bind to the Column.
<data:DataGridTextColumn Header="FirstName" Width="SizeToHeader"
Binding="{Binding FirstName}" CanUserSort="true"
/>
Till this point you don't need to worry much, but have you ever faced a scenario
where you had a DataGrid with custom column with CanUserSort set to true, but still
sorting did not work. The reason being in case of standard grid the property used
for binding and sorting is same. But when you create a custom column there is no
way for the column to figure out on what basis the sorting should occur, as a result
the sorting does not work. In this case if you want to enable sorting, you need
to assign the property nameto SortMemberPath
attribute of DataGridTemplateColumn.
<data:DataGridTemplateColumn Header="Name"
CanUserSort="true" SortMemberPath="DisplayName">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding DisplayName}"/>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
Happy Coding :)