Bind to Count of items in the DataContext

后端 未结 5 1235
别跟我提以往
别跟我提以往 2021-02-12 03:45

I want to bind to the Count/amount of items within my DataContext.

I have an object, lets say person which has a List

as a property. I would
5条回答
  •  野性不改
    2021-02-12 03:57

    In my case, Robert's answer got me really close to what I was after. However, I'm adding many items to the list from a service call, and didn't want the event firing for each item. To this end, I took advantage of functionality already built into BindingList:

    public class BindingListNotifyCount : BindingList, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected override void OnListChanged(ListChangedEventArgs e)
        {
            base.OnListChanged(e);
            RaisePropertyChanged("Count");
        }
    
        protected void RaisePropertyChanged([CallerMemberName]string propertyName = "") =>
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    

    Then I simply use it as:

    displayItems.RaiseListChangedEvents = false;
    serviceItems.ForEach(item => displayItems.Add(item));
    displayItems.RaiseListChangedEvents = true;
    displayItems.ResetBindings();
    

提交回复
热议问题