Monday 15 June 2015

c# - Update Parent when there is change in child -



c# - Update Parent when there is change in child -

this question has reply here:

how phone call functions in main view model other view models? 1 reply

parent collection binded radgridview hierarchical gridview structure.

is there way trigger parent property when there alter in inner collection

parent object:

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> /// or set id. /// </summary> public int32 id { { homecoming this.m_iid; } set { if (this.m_iid != value) { this.ischanged = true; this.m_iid = value; raisepropertychanged("id"); } } } /// <summary> /// or set name. /// </summary> public string name { { homecoming this.m_sname; } set { if (this.m_sname != value) { this.ischanged = true; this.m_sname = value; raisepropertychanged("name"); } } } /// <summary> /// or set covercontent collection. /// </summary> public observablecollection<covercontentbo> covercontentcollection { { homecoming this.m_ocovercontent; } set { if (this.m_ocovercontent != value) { this.ischanged = true; this.m_ocovercontent = value; raisepropertychanged("covercontentcollection"); } } } /// <summary> /// or set changed flag. /// </summary> public bool ischanged { { homecoming this.m_bischanged || this.covercontentcollection.any(i=>i.ischanged); } set { if (this.m_bischanged != value) { this.m_bischanged = value; raisepropertychanged("ischanged"); } } } #endregion }

and child object

[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> /// or set cover id. /// </summary> public int32 coverid { { homecoming this.m_icoverid; } set { if (this.m_icoverid != value) { this.ischanged = true; this.m_icoverid = value; raisepropertychanged("coverid"); } } } /// <summary> /// or set id. /// </summary> public int32 id { { homecoming this.m_iid; } set { if (this.m_iid != value) { this.ischanged = true; this.m_iid = value; raisepropertychanged("id"); } } } /// <summary> /// or set changed flag. /// </summary> public bool ischanged { { homecoming this.m_bischanged; } set { if (this.m_bischanged != value) { this.m_bischanged = value; raisepropertychanged("ischanged"); } } } #endregion }

in viewmodel

public observablecollection<coverhierarchical> coverhierarchicalcollection { { homecoming m_coverhierarchicalcollection; } set { if (m_coverhierarchicalcollection != value) { m_coverhierarchicalcollection = value; onpropertychanged(); } } }

coverhierarchicalcollection bound view. if user edits kid grid, changes happen in child. there way trigger ischanged property of parent, on changing kid inner collection (covercontentcollection) field.

you can hear propertychanged , collectionchanged of kid below , update ischanged of parent

/// <summary> /// or set covercontent collection. /// </summary> public observablecollection<covercontentbo> covercontentcollection { { homecoming 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; }

c# wpf mvvm telerik

No comments:

Post a Comment