How to notify property change for count of an ObservableCollection in windows phone 8?

1.8k views Asked by At

Let, I have an ObservableCollection. I m binding it's Count property with the Text property of a TextBlock. As one or more items added or deleted from the collection the Count get updated. As I know ObservableCollection implements both the INotifyPropertyChanged and INotifyCollectionChanged, so when the Count property is changed then my view should be updated. I am expecting the TextBlock that is bind with the Count property should show the updated count. But anyhow NotifyPropertyChange is not being called for the Count property!

Here how I am binding the Text property with Count:

<TextBlock Text="{Binding MyObservableCollection.Count}" />

Is there any way to notify property change for the Count property of an ObservableCollection?

3

There are 3 answers

0
Dinesh balan On

As For as I know ObservableCollection will update automatically in the cases of Add/Remove if you are Set a new value for entire ObservableCollection means you need to Raise the ObservableCollection in the setter of the property. So if you are setting new collection means add PropertyChangedEvent and if you are doing add/remove means don't need to Raise the property.If you are in second scenario Check your output window for Binding errors most probably that will be your problem.

0
Cologler On

other way:

you can add a Count to your view model, when you add or remove item from MyObservableCollection, you update Count manual, and invoke NotifyPropertyChange.

0
Oscar Emilio Perez Martinez On

A quick way is to use CollectionChanged eventhandler For example:

 public class ViewModelExample : INotifyPropertyChanged
 {

    public event PropertyChangedEventHandler PropertyChanged;
    private int _count;
    public int Count
    {
        get
        {
            return _count;
        }
        set
        {
            _count = value;
            RaisePropertyChanged("Count");
        }
    }

    private ObservableCollection<String> _myObservableCollection;
    public ObservableCollection<String> MyObservableCollection
    {
        get
        {
            return _myObservableCollection;
        }
        set
        {
            _myObservableCollection = value;
            RaisePropertyChanged("MyObservableCollection");
        }
    }

    public ViewModelExample()
    {
        this.MyObservableCollection = new ObservableCollection<String>();
        this.MyObservableCollection.CollectionChanged += this.OnCollectionChanged;
        this.Count = MyObservableCollection.Count;
        for(int j=0;j<20;j++)
        {
            this.MyObservableCollection.Add("SOMETHING HERE");
        }
    }

    private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if(e.NewItems!=null)
        {
           this.Count+=e.NewItems.Count;
        }
    }

    private void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

YOUR XAML

 <TextBlock FontSize="30">
            <TextBlock.Inlines>
                <Run Text="CURRENT COUNT="/>
                <Run Text="{Binding Count,Mode=TwoWay}"/>
            </TextBlock.Inlines>
        </TextBlock>

AND THEN YOU WILL GET THE FOLLOWING RESULT :)

enter image description here