LittleFunny
LittleFunny

Reputation: 8405

Xamarin form: IValueConverter receive null value in Convert method

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>

Upvotes: 4

Views: 1618

Answers (4)

Ejrr1085
Ejrr1085

Reputation: 1071

In NET MAUI 8 persist this bug

Upvotes: 0

Ghasan غسان
Ghasan غسان

Reputation: 5877

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.

Upvotes: 3

Timothy James
Timothy James

Reputation: 1102

Phatye is definitely correct in saying the line

BackgroundColor="{Binding ., Converter={StaticResource accountedToColorConverter}}"

is the culprit. I too have attempted to use the {Binding .} and {Binding Path=.} with a converter in the past only to run into the same null reference issues you're running into. It seems that Xamarin doesn't like this.

The proper solution will be to pass the proper path of the property you want to bind to:

Assuming the property is a top level property

BackgroundColor="{Binding Path=accounted, Converter={StaticResource accountedToColorConverter}}"

Otherwise you could do this:

BackgroundColor="{Binding Path=topLevelProperty.accounted, Converter={StaticResource accountedToColorConverter}}"

Upvotes: 2

Phatye
Phatye

Reputation: 21

BackgroundColor="{Binding ., Converter={StaticResource accountedToColorConverter}}" 

That line is the likely culprit. It will only be valid if the binding context of the page is that single "AccountedTo" property. Change it to "{Binding BackgroundProperty}" where "BackgroundProperty" is the "AccountedTo" value.

Upvotes: 2

Related Questions