Sorting
RadListView provides you with the functionality to programmatically sort its data.
This can be achieved through adding SortDescriptors to its RadListView.SortDescriptors collection.
There are two type of descriptors:
-
PropertySortDescriptor : You can sort the data by a property value from the class that defines your items. This descriptor exposes the following properties:
- PropertyName: Gets or sets the string name of the property that is used to retrieve the key to sort by.
- SortOrder: Changes the sort order to Ascending or Descending.
- Comparer: Gets or sets a custom IComparer instance. If this property is null, the default comparer will be used.
-
DelegateSortDescriptor : This descriptor enables you to sort by a custom key(e.g. some complex expression combining two or more properties) instead of being limited by the value of a single property of your item. This descriptor exposes the following properties:
- KeyLookup: Gets or sets the IKeyLookup instance used to retrieve the sort key for each data item.
- SortOrder: Changes the sort order to Ascending or Descending.
- Comparer: Gets or sets a custom IComparer instance. If this property is null, the default comparer will be used.
Example
Example 1: Defining the item model and key lookup
public class Item
{
public string Name { get; set; }
public double Id { get; set; }
}
public class Key : IKeyLookup
{
public object GetKey(object instance)
{
return (instance as Item).Id % 2;
}
}
Example 2: Populating with data
public MainWindow()
{
this.InitializeComponent();
List<Item> list = new List<Item>();
for (int i = 0; i < 10; i++)
{
list.Add(new Item { Id = i, Name = "Item " + i });
}
this.DataContext = list;
}
Example 3: Adding a sort descriptor in XAML
<telerikData:RadListView ItemsSource="{Binding}">
<telerikData:RadListView.Resources>
<local:Key x:Key="key" />
</telerikData:RadListView.Resources>
<telerikData:RadListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</telerikData:RadListView.ItemTemplate>
<telerikData:RadListView.SortDescriptors>
<dataCore:DelegateSortDescriptor SortOrder="Descending" KeyLookup="{StaticResource key}" />
</telerikData:RadListView.SortDescriptors>
</telerikData:RadListView>
Example 4: Telerik namespace definitions
xmlns:telerikData="using:Telerik.UI.Xaml.Controls.Data"
xmlns:dataCore="using:Telerik.Data.Core"