Xamarin form: IValueConverter receive null value in Convert method

孤人 提交于 2021-01-27 21:29:35

问题


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>

回答1:


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}}"




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/44103919/xamarin-form-ivalueconverter-receive-null-value-in-convert-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!