子项发生更改时更新父项
本文关键字:更新 | 更新日期: 2023-09-27 18:29:46
父集合绑定到具有分层网格视图结构的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
}
In-ViewModel
public ObservableCollection<CoverHierarchical> CoverHierarchicalCollection
{
get { return m_CoverHierarchicalCollection; }
set
{
if (m_CoverHierarchicalCollection != value)
{
m_CoverHierarchicalCollection = value;
OnPropertyChanged();
}
}
}
CoverHierarchicalCollection
已绑定到视图。如果用户编辑子网格,则子网格中会发生更改。在更改Child内部集合(CoverContentCollection
)字段时,是否有任何方法可以触发父级的IsChanged
属性。
您可以像下面这样收听子级的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;
}