c# - Collection changed event for child property -
i have use following code snippet creating observablecollection binded datagrid.
public class test:inotifypropertychanged { private string _name; public string name { { return _name; } set { _name = value;onpropertychanged("name"); } } private string _city; public string city { { return _city; } set { _city = value;onpropertychanged("city");} } #region inotifypropertychanged members public event propertychangedeventhandler propertychanged; public void onpropertychanged(string propertyname) { propertychangedeventhandler handler = propertychanged; if (handler != null) { var e = new propertychangedeventargs(propertyname); handler(this, e); } } #endregion } class data:inotifypropertychanged { private int customerid; public int customerid { { return customerid; } set { customerid = value; onpropertychanged("customerid"); } } private bool isselected; public bool isselected { { return isselected; } set { isselected = value; onpropertychanged("isselected"); } } private observablecollection<test> _collection; public observablecollection<test> collection { { return _collection; } set { _collection = value;onpropertychanged("collection" + ""); } } public event propertychangedeventhandler propertychanged; public void onpropertychanged(string propertyname) { propertychangedeventhandler handler = propertychanged; if (handler != null) { var e = new propertychangedeventargs(propertyname); handler(this, e); } } } class viewmodel:notificationobject { public viewmodel() { this.gdcsource = getsource(); } private observablecollection<data> _gdcsource; public observablecollection<data> gdcsource { { return _gdcsource; } set { _gdcsource = value; raisepropertychanged("gdcsource");} } private observablecollection<data> getsource() { observablecollection<data> items = new observablecollection<data>(); if (items != null) { items.add(new data() { isselected = true, customerid = 1, }); items.add(new data() { isselected = true, customerid = 2, }); } return items; } } public partial class mainwindow : window { public mainwindow() { initializecomponent(); viewmodel vmmodel = new viewmodel(); this.datagrid.itemssource = vmmodel.gdcsource; vmmodel.gdcsource.collectionchanged += new system.collections.specialized.notifycollectionchangedeventhandler(gdcsource_collectionchanged); } void gdcsource_collectionchanged(object sender, system.collections.specialized.notifycollectionchangedeventargs e) { //listen collection changed event underlying source } // add object collection property private void test_onclick(object sender, routedeventargs e) { (this.datacontext viewmodel).gdcsource[0].collection.add(new test() { name = "name1", city = "city1" }); (this.datacontext viewmodel).gdcsource[0].collection.add(new test() { name = "name1", city = "city1" }); (this.datacontext viewmodel).gdcsource[0].collection.add(new test() { name = "name1", city = "city1" }); } }
it possible listen while adding collection property in event.
thanks in advance
regards, rajasekar
if mean want register event raised when item added/deleted in observable collection should @ collectionchanged event
observablecollection<t>.collectionchanged event
occurs when item added, removed, changed, moved, or entire list refreshed.
Comments
Post a Comment