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