Forum Xamarin.Forms

Change parent background through Converter when entry text changes

Hi, I'm very new to xamarin in general and I'm having some trouble figuring out how to make a piece of code work with it.

I have an Entry and two Buttons inside a ListView to change the quantity of the products I show in this list. What I'm trying to do is use a converter to change the background of the Stacklayout that surrounds these fields when the quantity is not 0. It works fine if I change the quantity with the buttons (they call a function that changes it in the viewmodel), but the converter is never called when I try writing a new value on the Entry. Can anyone tell me what Im missed here?

This is my XAML:

<ListView ItemsSource="{Binding Products1}" ItemTapped="listProducts_ItemTapped">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition></ColumnDefinition>
                            <ColumnDefinition></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition></RowDefinition>
                        </Grid.RowDefinitions>

                        <StackLayout Grid.Row="0" Grid.Column="0" BackgroundColor="{Binding Quantity, Converter={StaticResource Key=OrderProductQuantityToColorConverter}}">
                            <Label HorizontalTextAlignment="Start" Text="{Binding Name}"/>
                        </StackLayout>
                        <StackLayout Grid.Row="0" Grid.Column="1" BackgroundColor="{Binding Quantity, Mode=TwoWay, Converter={StaticResource Key=OrderProductQuantityToColorConverter}}">
                            <Button Clicked="BtnProductQuantityMinus_Clicked" CommandParameter="{Binding}"/>
                            <Entry Keyboard="Numeric" Text="{Binding Quantity}"/>
                            <Button Clicked="BtnProductQuantityPlus_Clicked" CommandParameter="{Binding}"/>
                        </StackLayout>
                    </Grid>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

And my converter:

class OrderProductQuantityToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var quantity = (int)value;

        var color = Color.White;
        if(quantity > 0)
        {
            color = Color.FromHex("#CCE6CC");
        }
        return color;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if(value != null)
        {
            return Color.FromHex("#CCE6CC");
        }
        return Color.White;
    }
}

Posts

  • CharllieACharllieA USMember ✭✭

    Did you try the TwoWay binding mode ?

  • Yes, I tried using the Mode=TwoWay in all of the callings to Quantity like this:

    <StackLayout Grid.Row="0" Grid.Column="0" BackgroundColor="{Binding Quantity, Mode=TwoWay, Converter={StaticResource Key=OrderProductQuantityToColorConverter}}">
        <Label HorizontalTextAlignment="Start" Text="{Binding Name}"/>
    </StackLayout>
    <StackLayout Grid.Row="0" Grid.Column="1" BackgroundColor="{Binding Quantity, Mode=TwoWay, Converter={StaticResource Key=OrderProductQuantityToColorConverter}}">
        <Button Clicked="BtnProductQuantityMinus_Clicked" CommandParameter="{Binding}"/>
        <Entry Keyboard="Numeric" Text="{Binding Quantity, Mode=TwoWay}"/>
        <Button Clicked="BtnProductQuantityPlus_Clicked" CommandParameter="{Binding}"/>
    </StackLayout>
    

    But it still doesn't work.

Sign In or Register to comment.