父集合绑定到具有分层网格视图结构的radGridView。
当内部集合
发生更改时,是否有任何方法可以触发父属性父对象:
public class CoverHierarchical : EditableModelBase<CoverHierarchical>
{
#region Attribute Declarations
private Int32 m_iId;
private string m_sName = "";
private bool m_bIsChanged;
private ObservableCollection<CoverContentBO> m_oCoverContent;
#endregion
/// <summary>
/// Get or set the id.
/// </summary>
public Int32 Id
{
get { return this.m_iId; }
set
{
if (this.m_iId != value)
{
this.IsChanged = true;
this.m_iId = value;
RaisePropertyChanged("Id");
}
}
}
/// <summary>
/// Get or set the name.
/// </summary>
public string Name
{
get { return this.m_sName; }
set
{
if (this.m_sName != value)
{
this.IsChanged = true;
this.m_sName = value;
RaisePropertyChanged("Name");
}
}
}
/// <summary>
/// Get or set the CoverContent Collection.
/// </summary>
public ObservableCollection<CoverContentBO> CoverContentCollection
{
get { return this.m_oCoverContent; }
set
{
if (this.m_oCoverContent != value)
{
this.IsChanged = true;
this.m_oCoverContent = value;
RaisePropertyChanged("CoverContentCollection");
}
}
}
/// <summary>
/// Get or set the changed flag.
/// </summary>
public bool IsChanged
{
get { return this.m_bIsChanged || this.CoverContentCollection.Any(i=>i.IsChanged); }
set
{
if (this.m_bIsChanged != value)
{
this.m_bIsChanged = value;
RaisePropertyChanged("IsChanged");
}
}
}
#endregion
}
和子对象
[Serializable]
public class CoverContentBO : EditableModelBase<CoverContentBO>
{
#region Attribute Declarations
private Int32 m_iCoverId;
private Int32 m_iId;
private bool m_bIsChanged;
#endregion
#region Public Properties
/// <summary>
/// Get or set the cover id.
/// </summary>
public Int32 CoverId
{
get { return this.m_iCoverId; }
set
{
if (this.m_iCoverId != value)
{
this.IsChanged = true;
this.m_iCoverId = value;
RaisePropertyChanged("CoverId");
}
}
}
/// <summary>
/// Get or set the id.
/// </summary>
public Int32 Id
{
get { return this.m_iId; }
set
{
if (this.m_iId != value)
{
this.IsChanged = true;
this.m_iId = value;
RaisePropertyChanged("Id");
}
}
}
/// <summary>
/// Get or set the changed flag.
/// </summary>
public bool IsChanged
{
get { return this.m_bIsChanged; }
set
{
if (this.m_bIsChanged != value)
{
this.m_bIsChanged = value;
RaisePropertyChanged("IsChanged");
}
}
}
#endregion
}
在ViewModel中
public ObservableCollection<CoverHierarchical> CoverHierarchicalCollection
{
get { return m_CoverHierarchicalCollection; }
set
{
if (m_CoverHierarchicalCollection != value)
{
m_CoverHierarchicalCollection = value;
OnPropertyChanged();
}
}
}
CoverHierarchicalCollection
绑定到视图。如果用户编辑子网格,则会在子级中进行更改。有没有办法在更改Child内部集合(IsChanged
)字段时触发parent的CoverContentCollection
属性。
答案 0 :(得分:1)
您可以像下面这样倾听孩子的PropertyChanged
和CollectionChanged
,并更新父母的IsChanged
/// <summary>
/// Get or set the CoverContent Collection.
/// </summary>
public ObservableCollection<CoverContentBO> CoverContentCollection
{
get { return this.m_oCoverContent; }
set
{
if (this.m_oCoverContent != value)
{
this.IsChanged = true;
RemoveEventhandlers(m_oCoverContent);
this.m_oCoverContent = value;
AddEventhandlers(m_oCoverContent);
RaisePropertyChanged("CoverContentCollection");
}
}
}
private void AddEventhandlers(ObservableCollection<CoverContentBO> oCoverContent)
{
foreach (var CoverContentBO in oCoverContent)
{
CoverContentBO.PropertyChanged +=CoverContentBO_PropertyChanged;
}
oCoverContent.CollectionChanged +=oCoverContent_CollectionChanged;
}
void oCoverContent_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
foreach (CoverContentBO CoverContentBO in e.OldItems)
{
CoverContentBO.PropertyChanged -=CoverContentBO_PropertyChanged;
}
foreach (CoverContentBO CoverContentBO in e.NewItems)
{
CoverContentBO.PropertyChanged +=CoverContentBO_PropertyChanged;
}
}
void CoverContentBO_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "IsChanged")
{
IsChanged = true;
}
}
private void RemoveEventhandlers(ObservableCollection<CoverContentBO> oCoverContent)
{
foreach (var CoverContentBO in oCoverContent)
{
CoverContentBO.PropertyChanged -=CoverContentBO_PropertyChanged;
}
oCoverContent.CollectionChanged -=oCoverContent_CollectionChanged;
}