Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Binding text to ObservableCollection's count

EliranEliran USMember ✭✭✭

I bind a Label's text to ObservableCollection which resides in my viewModel.
The label needs to display the amount of items in that ObservableCollection. Therefor I use a converter.
(I don't want to add unnecessary properties to the viewModel).

Of some reason it works only in the beginning, but not when I remove items from the collection.
When I remove items from the collection, the converter is not even being touched (checked it by breaking points).

This is the collection in the viewModel

 public class MainPageViewModel: INotifyPropertyChanged
 {
        ....
        public ObservableCollection<StoreVs> itemsInStore { get; set; }
        ......
}

This is the Xaml:

    <ContentPage.Resources>
        <convertersPrefix:CollectionToIntConverter x:Key="myCollectionToIntConverter"/>
    </ContentPage.Resources>
          ......                
     <Label  Text="{Binding itemsInStore , Converter={StaticResource myCollectionToIntConverter} ,Mode=OneWay}"/>

and this is the converter:

 class CollectionToIntConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            ObservableCollection<StoreVs> observableCollection = value as ObservableCollection<StoreVs>;
            return observableCollection.Count.ToString();
        }

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

Best Answer

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai
    edited September 21

    When the items in itemsInStore has changed, it only fired the INotifyCollectionChanged.
    This interface won't notify the xaml to change the value of a single property. the {Binding itemsInStore ...} in your case.
    We have to manually call the propertyChanged event to change the value.
    If you manipulate the items in the view model, try to call:

    // Adding or deleting the items
    OnPropertyChanged("itemsInStore");
    

    Outside the view model:

    // viewModel.itemsInStore.Add(new StoreVs());
    viewModel.OnPropertyChanged("itemsInStore");
    

    I saw you have implemented the INotifyPropertyChanged so the OnPropertyChanged here was:

    public void OnPropertyChanged(string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    
  • EliranEliran USMember ✭✭✭

    @LandLu Thank you for your reply.

    @LandLu said:
    When the items in itemsInStore has changed, it only fired the INotifyCollectionChanged.

    I think that when items in 'itemsInStore' changes, it only fires the 'INotifyPropertyChanged' -
    not the INotifyCollectionChanged.
    INotifyCollectionChanged is being fired only when you add/remove items, or when a reference
    in one of the collection's cells is pointing to another object (or maybe this is what you meant
    when you wrote "items in 'itemsInStore' changes").

    @LandLu said:

    This interface won't notify the xaml to change the value of a single property. the {Binding itemsInStore ...} in your case.
    We have to manually call the propertyChanged event to change the value.
    If you manipulate the items in the view model, try to call:

    // Adding or deleting the items
    OnPropertyChanged("itemsInStore");
    

    This is something I didn't understand. When I have a NON collection property
    I implement INotifyPropertyChanged.
    When I have a collection, I need to implement INotifyCollectionChanged and not INotifyPropertyChanged.
    Therefor I use ObservableCollection which is already implements INotifyCollectionChanged interface.
    So when an item is being removed from observable collection, an event is being fired.
    why can't I catch this event in a converter?

  • EliranEliran USMember ✭✭✭

    @LandLu said:
    It depends on which bindable property you are binding.

    I didn't now this ! ! Thank you very much B)

Sign In or Register to comment.