Consider a scenario where you want to group by a number. Unless you have a short list of clean integers, the group of unique values will be unmanageable. You would instead want to apply a custom grouping action that groups items into ranges like "between 0 and 100" and "over 5,000", etc. To do this you would perform a custom grouping passing an IValueConverter to the PropertyGroupDescription parameter.
For example, the following code will group our Customer collection by Country listing each group as a letter of the alphabet (such as: Countries: A, Countries: B, Countries: C and so on). The Countries: A group would include all items belonging to Algeria, Argentina, and Austria. Modify the previous code snippet to the following:
Visual Basic |
Copy Code
|
---|---|
_view.GroupDescriptions.Add(New C1.Xaml.PropertyGroupDescription("Country", New GroupByCountryAtoZConverter())) |
C# |
Copy Code
|
---|---|
_view.GroupDescriptions.Add(new C1.Xaml.PropertyGroupDescription("Country", new GroupByCountryAtoZConverter())); |
And add the following GroupByCountryAtoZConverter class to your project:
Visual Basic |
Copy Code
|
---|---|
Public Class GroupByCountryAtoZConverter Implements IValueConverter Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As String) As Object If value IsNot Nothing Then Return value.ToString()(0) End If Return "Undefined" End Function Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As String) As Object Throw New NotImplementedException() End Function End Class |
C# |
Copy Code
|
---|---|
public class GroupByCountryAtoZConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string culture) { if (value != null) { return value.ToString()[0]; } return "Undefined"; } public object ConvertBack(object value, Type targetType, object parameter, string culture) { throw new NotImplementedException(); } } |