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>
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.
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}}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With