Xamarin form: IValueConverter receive null value in Convert method

Viewed 1302

I have a list view set with item source as child. I want to bind a child object to a view which will set the color through the converter.

The converter method got called but the value i passed in was null.

Apart from dot, I also use Path=/ but the value passed to the converter still null. If i bind the property, it's fine but not the current item.

<ListView x:Name="childListView" 
    VerticalOptions="FillAndExpand" 
    HasUnevenRows="true" 
    ItemSelected="OnItemSelected"
    ItemTapped="OnItemTapped">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                    <StackLayout 
                        BackgroundColor="{Binding ., Converter={StaticResource accountedToColorConverter}}" 
                        Spacing="0" Padding="0" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
                        <StackLayout Orientation="Horizontal" Spacing="10" Padding="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                            <StackLayout Orientation="Horizontal" HorizontalOptions="StartAndExpand">
                                <controls:CircleImage>
3 Answers

This is an interesting bit of behavior. I recently had this while working with CarouselView (Forms.Plugin), and doing more research, it turned out that the BindingContext of each of CarouselView element is set more than once for some reason.

So the first time, the converter gets null value, but eventually, it gets called a second time with the correct value, so I changed my converter to gracefully handle null values, and it worked.

Related