Bogdan Constantin
Bogdan Constantin

Reputation: 85

How to bind BackgroundColor of ListView child to value?

I am trying to set the background of a child of a ListView based on the some value.

I tried using a ValueConverter, but it would only update the color when the item is redrawn on the screen. I checked with the debuger, and the Convert method is called, and the right value is returned.

IValue.Convert implementation

 return Convert.ToInt32(value) > 0 ? "#E0EBFF" : "#FFFFFF";

XAML file:

<ContentPage.Resources>
        <converters:QuantityToColorConverter x:Key="QuantityToColorConverter"></converters:QuantityToColorConverter>
</ContentPage.Resources>
<ListView > <!-- Options not included for simplicty -->
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <ViewCell.View BackgroundColor="#ecf0f1" >
                                    <Grid Margin="0,0,0,10" BackgroundColor="{Binding Path=Quantity,Converter={StaticResource QuantityToColorConverter}}" RowSpacing="0" ColumnSpacing="0">

The background color should change but it stays the same until the child is scrolled past and then scrolled back to

Upvotes: 0

Views: 48

Answers (1)

Guilherme Nimer
Guilherme Nimer

Reputation: 555

Try this.

return Convert.ToInt32((value as "Here you put the right type of property").Text)) > 0 ? "" : ""

Upvotes: 1

Related Questions