BindingList和嵌套属性

时间:2013-01-18 16:31:20

标签: c# inotifypropertychanged nested bindinglist

我有一个跟踪视频流的类a,为简单起见,我使用自动属性对子类中的属性进行分组以访问它们。然后我将整个类绑定到BindingList,但只显示None Nested Properties。如何才能显示嵌套属性?

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");
        }
    }
....
}

2 个答案:

答案 0 :(得分:1)

您需要触发父类型的OnPropertyChanged,而不是子类型。

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");
            }
        }
    }
}

Stream置于SubCodec的构造函数中并且不允许更改它可能更简单。这将是避免我在Codec集合方法的评论中提到的问题的一种方法。

答案 1 :(得分:0)

您需要在PropertyChanged

上举起SubCodec个活动
private SubCoded _codec;
internal SubCodec Codec 
{
      get {return _codec;}  
      set 
      {
            _codec = value;
            OnPropertyChanged("Codec");
       }
 }