0

I have an IValueConverter that returns true upon the provided ObservableCollection having a count of 3 or greater. This works great when the list is initialized to a specific count, but the moment it is modified to a point where the IValueConverter should switch from true to false or vice versa, it does not update.

IValueConverter

namespace LemonadeClient.UI.Converters
{
    class HasValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            //cast it to a list
            IList col = (IList)value;
            //if it doesn't have anything return true
            return col.Count >= 3;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

XAML

<Label
    HorizontalOptions="Center"
    IsVisible="{Binding HistoryList, Converter={StaticResource HasValueConverter}}"
    Text="Testing!" />

Is there a way to "force" IValueConverter to update? I'm not sure on how to approach this.

Scornz
  • 380
  • 3
  • 14
  • I'd guess that Label is only monitoring PropertyChanged events, not CollectionChanged events – Jason Aug 20 '20 at 17:00
  • @Jason Is there a way to change that? – Scornz Aug 20 '20 at 17:00
  • 1
    create a class that inherits ObservableCollection and INotifyPropertyChanged, and have it fire a PropertyChanged event whenever CollectionChanged is fired – Jason Aug 20 '20 at 17:04
  • The converter is called any time the bound value changes. You've bound the _collection reference_ itself, so only if the collection itself changes would the binding be updated. The literal answer to your question is found in the duplicate. However, since one way or the other you're going to have to either monitor the collection's `CollectionChanged` event or add logic to wherever the collection might be changed, you really might as well just make a `bool` property in your view model and update _that_, instead of using a converter and forcing the binding to refresh. – Peter Duniho Aug 20 '20 at 18:49

0 Answers0