Binding Converters

Version: v9.x

This code is an example of how to use binding converters in FmgLib.MauiMarkup.

A CollectionView is defined and for each item in the MyNumbers 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.Green or Colors.Yellow based on the result.

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

    public CustomPage()
    {
	    this
	    .Content(
			new VerticalStackLayout()
			Children(
				new CollectionView()
                .ItemsSource(MyNumbers)
                .ItemTemplate(() => 
                    new Label()
                        .FontSize(30)
                        .Text(e => e.Path("."))
                        .TextColor(Colors.Gray)
                        .BackgroundColor(e => e
                            .Path(".")
                            .Convert((int n) => n % 2 == 0 ? Colors.Green : Colors.Yellow)
                        )
                )
			)
		);
    }
}