【问题标题】:BindingList and Nested PropertiesBindingList 和嵌套属性
【发布时间】:2013-01-02 10:06:52
【问题描述】:

我有一个跟踪视频流的类 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");
        }
    }
....
}

【问题讨论】:

    标签: c# inotifypropertychanged nested bindinglist


    【解决方案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 set 方法的评论中提到的问题的一种方法。

    【讨论】:

      【解决方案2】:

      您需要在SubCodec 上引发PropertyChanged 事件

      private SubCoded _codec;
      internal SubCodec Codec 
      {
            get {return _codec;}  
            set 
            {
                  _codec = value;
                  OnPropertyChanged("Codec");
             }
       }
      

      【讨论】:

        猜你喜欢
        • 2013-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多