1

I wan't change the background color in the selected record. I create converter but it marks out more records than one. I need only one record to be selected and change the background color in it

ColorConverter

    public class ColorElementSelectionConverter : ElementSelectionConverter<ColorResponseModel>
{
    protected override bool Equals(ColorResponseModel selectedElement, ColorResponseModel currentElemnt)
    {
        return selectedElement.Id.Equals(currentElemnt.Id);
    }
}

ElementSelectConverter

    public abstract class ElementSelectionConverter<T> : IValueConverter where T: class
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var selectedElement = value as T;
        if(selectedElement == null) return false;

        var viewCell = parameter as ViewCell;
        if(viewCell == null) return false;

        var currentElemnt = viewCell.BindingContext as T;
        if(currentElemnt == null) return false;

        return Equals(selectedElement, currentElemnt);
    }

    protected virtual bool Equals(T selectedElement, T currentElemnt)
    {
        return selectedElement.Equals(currentElemnt);
    }

View

 <ViewCell x:Name="ColorViewCell">
                    <Grid Margin="0,0,0,0" HeightRequest="36" VerticalOptions="Center">
                        <Grid.Triggers>
                            <DataTrigger TargetType="Grid" 
                                         Binding="{Binding Path=BindingContext.Color, Source={x:Reference Name=CarColorListPopup},Converter={StaticResource ColorElementSelectionConverter},
                                ConverterParameter={x:Reference Name=ColorViewCell}}" Value="true">
                                <Setter Property="BackgroundColor" Value="#388FEE" />
                            </DataTrigger>
Sp3ctre
  • 129
  • 1
  • 8

2 Answers2

1

I was using ItemSelected, but the selected item wasn't getting deselected unless until I select any other item in the ListView. I replace ItemSelected by ItemTapped.

Dev007
  • 366
  • 1
  • 12
0

To change background color of selected item in list view, find the solution provided here:

Change background color of selected listview item

You need to create custom viewcell and its renderer for the same.

Hope this helps you!

MShah
  • 1,247
  • 9
  • 14