INotifyPropertyChanged for Count property in WPF?

一个人想着一个人 提交于 2020-02-03 01:57:11

问题


I have a textbox which is bound to an Int property in my viewmodel which gets a count of an ObservableCollection. Here is my Xaml binding:

Text="{Binding ArticleCount, Mode=OneWay}" 

And my int property:

 private Int32 _ArticleCount;
    public int ArticleCount
    {
        get
        {                
            if (this.ModelviewArticleObservableList == null)
            {
                return 0;
            }
            else
            {
                return this.ModelviewArticleObservableList.Count;                  
            }               
        } 
    }

This works fine on application initialisation and I get the ObservableCollection count in my UI TextBox. However there are methods that then change the ObservableCollection and therefore the count changes. I need a Property Changed event to notify the when the count has changed but I'm not sure on how to do this? Here is my ObservableCollection property:

 private ObservableCollection<viewArticle> _ModelviewArticleObservableList = new ObservableCollection<viewArticle>();
    public ObservableCollection<viewArticle> ModelviewArticleObservableList
    {
        get { return _ModelviewArticleObservableList; }
        set
        {
            _ModelviewArticleObservableList = value;
            OnPropertyChanged("ModelviewArticleObservableList");                
        }
    }

I currently have a normal implementation of INPC:

public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

回答1:


You have to raise the PropertyChanged event every time the number of elements in the collections changes. A good way is to subscribe to the CollectionChanged event of the ObservableCollection.

private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    // you could examine e to check if there is an actual change in the count first
    OnPropertyChanged("ArticleCount");
}

Additionally there is the case that the collection itself is set to null or a different value, in which case your ArticleCount property should change too. You also have to remove your CollectionChanged event handler from the old collection and add it to the new one.

public ObservableCollection<viewArticle> ModelviewArticleObservableList
{
    get { return _ModelviewArticleObservableList; }
    set
    {
        if (_ModelviewArticleObservableList != null)
        {
            _ModelviewArticleObservableList.CollectionChanged -= OnCollectionChanged;
        }

        _ModelviewArticleObservableList = value;

        if (_ModelviewArticleObservableList != null)
        {
            _ModelviewArticleObservableList.CollectionChanged += OnCollectionChanged;
        }

        OnPropertyChanged("ModelviewArticleObservableList");
        OnPropertyChanged("ArticleCount");
    }
}



回答2:


Is ModelviewArticleObservableList a publicly visible property of your ViewModel?

If so, you can bind your TextBox directly to it's Count property.

Text="{Binding Path=ModelviewArticleObservableList.Count}" 

You need the "Path=" as you're not binding to a direct property. The "Mode=OneWay" is unnecessary.

ObservableCollection implements INotifyPropertyChanged, and the Count property will be notified for each update to the collection.

By the way, why are you using a TextBox rather than TextBlock for a value that can't be edited in the UI?



来源:https://stackoverflow.com/questions/24180519/inotifypropertychanged-for-count-property-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!