PropertyChanged for nested class structure

I have the following class structure

class Top : NotifyPropertyChanged
{
   private List<Inner>  innerList;

   public bool IsInnerTrue
   {
      get
      {
          foreach (Inner inner in innerList)
          {
             if (inner.IsTrue)
                return true;
          }
          return false;
      };
   }
}

class Inner : NotifyPropetyChanged
{
   private bool isTrue;
   public bool IsTure
   {
      get
      {
         return isTrue;
      }
      set
      {
         isTrue = value;
         NotifyPropretyChanged("IsTrue");      
      }
   }
}

      

In my opinion, I am binding to the IsInnerTrue property of the Top class. My problem is that I cannot figure out how to fire the PropertyChanged event for IsInnerTrue when the IsTrue property of one of the Inner objects changes value.

Does anyone have a suggestion without building an event handler for each Inner Object?

+3


source to share


1 answer


Here are two options. One, subscribe to the internal PropertyChanged events:

class Top : NotifyPropertyChanged
{
    private List<Inner> innerList = new List<Inner>();

    public bool IsInnerTrue
    {
        get
        {
            foreach (Inner inner in innerList)
            {
                if (inner.IsTrue)
                    return true;
            }
            return false;
        }
    }

    public void Add(Inner inner)
    {
        innerList.Add(inner);
        inner.PropertyChanged += InnerPropertyChanged;
    }

    public void Remove(Inner inner)
    {
        innerList.Remove(inner);
        inner.PropertyChanged -= InnerPropertyChanged;
    }

    private void InnerPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "IsTrue")
            RaisePropertyChanged("IsInnerTrue");
    }
}

      



Or two, use a dependency tracking library like Update Controls . These libraries can detect that IsInnerTrue depends on IsTrue and will fire the top-level property change event when the internal property changes.

+3


source







All Articles