Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6031063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:07:42+00:00 2026-05-23T05:07:42+00:00

Some properties on my viewmodel : public ObservableCollection<Task> Tasks { get; set; } public

  • 0

Some properties on my viewmodel:

public ObservableCollection<Task> Tasks { get; set; }

public int Count
{
    get { return Tasks.Count; }
}

public int Completed
{
    get { return Tasks.Count(t => t.IsComplete); }
}

What’s the best way to update these properties when Tasks changes?

My current method:

public TaskViewModel()
{
    Tasks = new ObservableCollection<Task>(repository.LoadTasks());
    Tasks.CollectionChanged += (s, e) => 
        {
            OnPropertyChanged("Count");
            OnPropertyChanged("Completed");
        };
}

Is there a more elegant way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:07:43+00:00Added an answer on May 23, 2026 at 5:07 am

    With respect to Count, you don’t have to do this at all. Simply bind to Tasks.Count and your bindings will get notified of the change by the ObservableCollection.

    Completed is a different story, because this is outside of ObservableCollection. Still, from the level of the abstraction/interface, you really want Completed to be a property of that Tasks collection.

    For this, I think a better approach would be to create “sub” view-model for your Tasks property:

    public class TasksViewModel : ObservableCollection<Task>
    {
        public int Completed
        {
            get { return this.Count(t => t.IsComplete); }
        }
    
        protected override void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            if(e.PropertyName == "Count") NotifyCompletedChanged();
        }
    
        protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            base.OnCollectionChanged(e);
            NotifyCompletedChanged();
        }
    
        void NotifyCompletedChanged()
        {
            OnPropertyChanged(_completedChangedArgs);
        }
        readonly PropertyChangedEventArgs _completedChangedArgs = new PropertyChangedEventArgs("Completed");
    }
    

    This gives you all of the benefits of the ObservableCollection, and effectively makes the Completed property part of it. We still haven’t captured only the cases where the number of completed items truly changes, but we have reduced the number of redundant notifications somewhat.

    Now the viewmodel just has the property:

    public TasksViewModel Tasks { get; set; }
    

    …and you can bind to Tasks, Tasks.Count, and Tasks.Completed with ease.


    As an alternative, if you would rather create these other properties on the “main” view-model, you can take this notion of a subclassed ObservableCollection<T> to create one with some method where you can pass in an Action<string> delegate, which would represent raising a property change notification on the main view-model, and some list of property names. This collection could then effectively raise the property change notifications on the view-model:

    public class ObservableCollectionWithSubscribers<T> : ObservableCollection<T>
    {
        Action<string> _notificationAction = s => { }; // do nothing, by default
        readonly IList<string> _subscribedProperties = new List<string>();
    
        public void SubscribeToChanges(Action<string> notificationAction, params string[] properties)
        {
            _notificationAction = notificationAction;
    
            foreach (var property in properties)
                _subscribedProperties.Add(property);
        }
    
    
        protected override void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            NotifySubscribers();
        }
    
        protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            base.OnCollectionChanged(e);
            NotifySubscribers();
        }
    
        void NotifySubscribers()
        {
            foreach (var property in _subscribedProperties)
                _notificationAction(property);
        }
    }
    

    You could even leave the property type as ObservableCollection<Task>.

    public class ViewModel : INotifyPropertyChanged
    {
        public ViewModel()
        {
            var tasks = new ObservableCollectionWithSubscribers<Task>();
            tasks.SubscribeToChanges(Notify, "Completed");
            Tasks = tasks;
        }
    
        public ObservableCollection<Task> Tasks { get; private set; }
    
        public int Completed
        {
            get { return Tasks.Count(t => t.IsComplete); }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        void Notify(string property)
        {
            var handler = PropertyChanged;
            if(handler != null) handler(this, new PropertyChangedEventArgs(property));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm iterating through some objects and trying to set some properties that, apparently, don't
I've got a simple class defined as: public class MyClass { //Some properties public
I have a ViewModel that encapsulates some properties that are being edited in an
I have ViewModel that has inside a Model and some extra properties. There are
Consider, for example: public interface IStaff { FullName name { get; set; } EMailAddress
I have a ViewModel : public class VM { public ObservableCollction<PersonRole> PersonRoles { get;
I read some properties from an xml file, amongst which is a string that
I want to change some properties of a LINQ query result object without creating
I want to define some properties on a class using the [Indexable()] attribute in
I have a class that has some properties. And I want something that calculates

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.