Search code examples
c#inotifypropertychangednestedbindinglist

BindingList and Nested Properties


I have class a that keeps track of video streams, and for simplicity I group like properties in a sub classes using auto properties to access them. I then bound the whole class to an BindingList, but only the None Nested Properties show up. How can i get the Nested Properties to show up also?

public class Stream: : INotifyPropertyChanged 
{
public bool InUse {
    get { return _inUse; }
    set {
        _inUse = value;
        OnPropertyChanged("InUse");
        }
    }
}
....
internal SubCodec Codec { get; set; }
internal class SubCodec
{
    public string VideoCodec 
    {
        get { return _audioCodec; }
        set {
            _audioCodec = value;
            OnPropertyChanged("AudioCodec");
        }
    }
....
}

Solution

  • You need to fire OnPropertyChanged of the parent type, not on the child type.

    public class Stream : INotifyPropertyChanged
    {
        private SubCodec _codec;
        internal SubCodec Codec
        {
            get
            {
                return _codec;
            }
            set
            {
                _codec = value;
                //note that you'll have problems if this code is set to other parents, 
                //or is removed from this object and then modified
                _codec.Parent = this;
            }
        }
        internal class SubCodec
        {
            internal Stream Parent { get; set; }
    
            private string _audioCodec;
            public string VideoCodec
            {
                get { return _audioCodec; }
                set
                {
                    _audioCodec = value;
                    Parent.OnPropertyChanged("VideoCodec");
                }
            }
        }
    }
    

    It may be simpler to put the Stream in the constructor of SubCodec and not allow it to be changed. It would be one way of avoiding the problems I mention in the comment of the Codec set method.