Reversing an IEnumerable<T> with a binding converter in Xamarin.Forms
• Alwyn Lombaard
I wanted to bind a property on the viewmodel of type IEnumerable<T> to the ItemsSource property of a CollectionView and be able to reverse the order of the items displayed without having to reverse them in the viewmodel or code behind code.
Here’s how I did it:
Define an IValueConverter with generic type (T). T is the type of the items in the Enumerable (String, Int32, Object etc)
The generic type of the IEnumerable must be supplied to the converter.
Use Binding markup to supply the generic type argument using x:TypeArguments.
In this example the Notes property on the viewmodel is of type List<string> but it could be any IEnumerable<String>.
If the property was of type IEnumerable<int> for example List<int>: