locked
ListView and AlternatingRowBackground - How to Refresh? RRS feed

  • Question

  • User285028 posted

    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;
        }
    }
    
    Wednesday, January 25, 2017 2:39 PM

All replies

  • User180523 posted

    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

    Wednesday, January 25, 2017 2:49 PM
  • User285028 posted

    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));
        }
    }
    
    Wednesday, January 25, 2017 3:20 PM
  • User285028 posted

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

    Monday, February 6, 2017 7:40 AM
  • User362434 posted

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

    Tuesday, January 9, 2018 4:31 PM
  • User180523 posted

    @"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.

    Tuesday, January 9, 2018 4:48 PM