Skip to content

Latest commit

 

History

History
32 lines (28 loc) · 1.3 KB

bindingconverters.md

File metadata and controls

32 lines (28 loc) · 1.3 KB

Binding Converters

This code is an example of how to use binding converters in Sharp.UI.

A CollectionView is defined and for each item in the Numbers list, a label is created with text equal to the value of the item. The BackgroundColor property of the label is bound to the item using the Convert method, which takes in a function that converts the value of the item (an integer) to a color. In this case, the function checks if the number is even or odd, and returns either Colors.White or Colors.Black based on the result.

public class AlternativeBackroundPage : ContentPage
{
    private List<int> _nubmers = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    public List<int> Numbers => _nubmers;

    public AlternativeBackroundPage()
    {
        Content = new VStack
        {
            new CollectionView()
                .ItemsSource(Numbers)
                .ItemTemplate(() => 
                    new Label()
                        .FontSize(30)
                        .Text(e => e.Path("."))
                        .TextColor(Colors.Gray)
                        .BackgroundColor(e => e
                            .Path(".")
                            .Convert((int n) => n % 2 == 0 ? Colors.White : Colors.Black)
                        )
                )
        };
    }
}