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

  • Home
  • SEARCH
  • 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 7955435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:42:43+00:00 2026-06-04T03:42:43+00:00

I have an ObservableCollection<Data> Items Data has a Columns property on it which is

  • 0

I have an ObservableCollection<Data> Items

Data has a “Columns” property on it which is again an ObservableCollection<Column>.

A Column object has a boolean property called “IsActive”.

I have a case where I need to determine if all “Items” have the “Columns” property and if so,
all the columns should either have “IsActive” as true or false but not both.

The trick is i need to put this logic in CanExecute of a button.. I will need to make this as efficient and fast as possible…Any ideas?
The struture is:

public class MyClass
{
     public ObservableCollection<Data> Items
    {
         get{return _items;}
     }
}

public class Data
{
    public ObservableCollection<Column> Columns
    {
         get{return _columns;}
     }
}

public class Column
{

   public bool IsActive{ get; set;}

}

Thanks!

  • 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-06-04T03:42:44+00:00Added an answer on June 4, 2026 at 3:42 am

    This smells like premature optimization. Have you measured the speed of a simplistic approach? Iterating through a few not too big collections will only take a few milliseconds on a modern CPU.

    If you want to use LINQ to compute if all Column objects are either active or inactive you can use this expression.

    public Boolean CanExecute {
      get {
        var aggregate = Items
          .SelectMany(i => i.Columns)
          .Aggregate(
            new {
              IsEmpty = true,
              AllAreActive = true,
              AllAreInactive = true
            },
            (a, c) => new {
              IsEmpty = false,
              AllAreActive = a.AllAreActive && c.IsActive,
              AllAreInactive = a.AllAreInactive && !c.IsActive
            }
        );
        return !aggregate.IsEmpty && (aggregate.AllAreActive || aggregate.AllAreInactive);
      }
    

    This code will iterate over all elements in all collections. You can improve on this by using a for loop and breaking it when both boolean variables becomes false. This can also by done in LINQ using TakeWhile using a predicate with side effects but a simple for loop is probably easier to understand.

    If you decide that a simplistic approach is too slow you need to keep track of the CanExecute property at the MyClass level. You can do this by setting up change notification handlers for all the ObservableCollection instances. It is somewhat tedious because you have two levels of collections but it will ensure that whenever a Column is added or removed from a collection or the IsActive property is changed the boolean variable backing CanExecute in MyClass is updated.

    Initially Column has to implement INotifyPropertyChanged:

    class Column : INotifyPropertyChanged {
    
      Boolean isActive;
    
      public Boolean IsActive {
        get { return this.isActive; }
        set {
          if (this.isActive == value)
            return;
          this.isActive = value;
          OnPropertyChanged("IsActive");
        }
      }
    
      public event PropertyChangedEventHandler PropertyChanged;
    
      protected void OnPropertyChanged(String propertyName) {
        var handler = PropertyChanged;
        if (handler != null)
          handler(this, new PropertyChangedEventArgs(propertyName));
      }
    
    }
    

    Data has to expose the desired property which for lack of better name I have called AllColumnsAreActiveOrInactive and changes to this property is signaled by implementing INotifyPropertyChanged.

    To track the status of all columns the CollectionChanged of the Column collection is handled. When a new Column is added the value of AllColumnsAreActiveOrInactive can be recomputed without iterating the Column collection. However, when IsActive changes on a single Column or when a Column is removed the collection has to be iterated to determine the new value of the AllColumnsAreActiveOrInactive.

    class Data : INotifyPropertyChanged {
    
      readonly ObservableCollection<Column> columns;
    
      Boolean allColumnsAreActive = true;
    
      Boolean allColumnsAreInactive = true;
    
      Boolean allColumnsAreActiveOrInactive = false;
    
      public Data() {
        this.columns = new ObservableCollection<Column>();
        this.columns.CollectionChanged += CollectionChanged;
      }
    
      public ObservableCollection<Column> Columns { get { return this.columns; } }
    
      public Boolean AllColumnsAreActiveOrInactive {
        get { return this.allColumnsAreActiveOrInactive; }
        set {
          if (value == this.allColumnsAreActiveOrInactive)
            return;
          this.allColumnsAreActiveOrInactive = value;
          OnPropertyChanged("AllColumnsAreActiveOrInactive");
        }
      }
    
      public event PropertyChangedEventHandler PropertyChanged;
    
      protected void OnPropertyChanged(String propertyName) {
        var handler = PropertyChanged;
        if (handler != null)
          handler(this, new PropertyChangedEventArgs(propertyName));
      }
    
      void CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) {
        if (e.Action == NotifyCollectionChangedAction.Reset) {
          RecomputeAllColumnsAreActiveOrInactive();
          return;
        }
        if (e.OldItems != null) {
          foreach (var item in e.OldItems.Cast<Column>())
            item.PropertyChanged -= ColumnPropertyChanged;
          RecomputeAllColumnsAreActiveOrInactive();
          return;
        }
        if (e.NewItems != null) {
          foreach (var column in e.NewItems.Cast<Column>()) {
            column.PropertyChanged += ColumnPropertyChanged;
            this.allColumnsAreActive = this.allColumnsAreActive && column.IsActive;
            this.allColumnsAreInactive = this.allColumnsAreInactive && !column.IsActive;
          }
          UpdateAllColumnsAreActiveOrInactive();
        }
      }
    
      void ColumnPropertyChanged(Object sender, PropertyChangedEventArgs e) {
        if (e.PropertyName == "IsActive") {
          var column = sender as Column;
          RecomputeAllColumnsAreActiveOrInactive();
        }
      }
    
      void RecomputeAllColumnsAreActiveOrInactive() {
        this.allColumnsAreActive = this.columns.All(c => c.IsActive);
        this.allColumnsAreInactive = this.columns.All(c => !c.IsActive);
        UpdateAllColumnsAreActiveOrInactive();
      }
    
      void UpdateAllColumnsAreActiveOrInactive() {
        AllColumnsAreActiveOrInactive = this.columns.Any()
          && (this.allColumnsAreActive || this.allColumnsAreInactive);
      }
    
    }
    
    class Column : INotifyPropertyChanged {
    
      Boolean isActive;
    
      public Boolean IsActive {
        get { return this.isActive; }
        set {
          if (this.isActive == value)
            return;
          this.isActive = value;
          OnPropertyChanged("IsActive");
        }
      }
    
      public event PropertyChangedEventHandler PropertyChanged;
    
      protected void OnPropertyChanged(String propertyName) {
        var handler = PropertyChanged;
        if (handler != null)
          handler(this, new PropertyChangedEventArgs(propertyName));
      }
    
    }
    

    To complete this solution the Column/Data collection approach has to be repeated for the Data/MyClass collection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a combobox which has ItemsSource set to an ObservableCollection property called DATA
i have function, which has to accept two types of data - Observable collection
I have a WPF DataGrid who's data source is an ObservableCollection. It is set
I have an ObservableCollection of Task objects. Each Task has the following properties: AssignedTo
Is it possible for me to have an ObservableCollection as a property in a
I would like to use a WrapPanel. I have a ObservableCollection with ViewModels which
I have a Silverlight delegate which gets an ObservableCollection in the EventArgs Result. The
ViewModel I have a property of type Member called KeyMember . The 'Member' type
I have a ObservableCollection which holds records of custom type Item. I use that
I have WPF combobox bound to a ObservableCollection ItemCategoryList <ComboBox Grid.Column=1 Grid.Row=2 Height=Auto HorizontalAlignment=Stretch

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.