ListView and AlternatingRowBackground - How to Refresh?

JesperJensenJesperJensen USMember ✭✭
edited January 2017 in Xamarin.Forms

I have a ListView of items, where i want the alternating rows to have a different background color.
I have achieved this, using a CustomConverter and then updating the BackgroundColor there.

Unfortunately, this is not updated after I delete an item from the Row.
Can anyone offer suggestions on how to force a Refresh or how to Trigger the Converter again?


I Added my Custom Converter as a Global Resources in my App.xaml file
<Color x:Key="BorderColor">#FF0000</Color>
<infrastructure:BackgroundConverter x:Key="bgColorPicker" OddRowColor="{StaticResource BorderColor}"/>

The Code for my BackgroundConverter class:
The ObservableProperty is a custom class the supplies the SetProperty function, to save code.

public class BackgroundConverter : ObservableProperty, IValueConverter
{
    Color _evenRowColor = Color.White;
    Color _oddRowColor = Color.Gray;
    public Color EvenRowColor
    {
        get { return _evenRowColor; }
        set { SetProperty(ref _evenRowColor, value, nameof(EvenRowColor)); }
    }

    public Color OddRowColor
    {
        get { return _oddRowColor; }
        set { SetProperty(ref _oddRowColor, value, nameof(OddRowColor)); }
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int index = 0;
        foreach (var itm in (parameter as ListView).ItemsSource)
        {
            if (itm != value)
                index++;
            else
            {
                return (index % 2 == 0) ? EvenRowColor : OddRowColor;
            }
        }
        return EvenRowColor;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return EvenRowColor;
    }
}

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I assume you have that backed with an ObserbableCollection and you're raising INotifyCollectionChanged in order to see added and removed items.

    Try raising INotifyPropertyChanged at the same time. In order words when you change an element tell your ViewModel the entire collection property has been set. That would cause the ListView to think its a completely new collection and re-render

  • JesperJensenJesperJensen USMember ✭✭

    Good idea, but unfortunately it did not work.

    I tried adding it after i removed the Line
    The List:
    public ObservableCollection<OrdreLinje> Linjer

    The Code to remove:

    internal void FjernLinje(object commandParameter)
    {
        var linje = commandParameter as OrdreLinje;
        if (linje != null)
        {
            linje.PropertyChanged -= Linje_PropertyChanged;
            Linjer.Remove(linje);
            RaisePropertyChanged(nameof(Linjer));
        }
    }
    
  • JesperJensenJesperJensen USMember ✭✭

    This issue is still haunting me. If anyone has any ideas, I would love to hear them.

  • Hola, me puedes decir como lograste hacer el binding del XAML al converter? te lo agradeceria mucho.
    Saludos.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited January 2018

    @www.joelramiro said:
    Hola, me puedes decir como lograste hacer el binding del XAML al converter? te lo agradeceria mucho.
    Saludos.

    Site guidlines state this is an English speaking site.
    Please post your comment in English.

Sign In or Register to comment.