c# - ListView not updated correctly with ObservableCollection -
i'm using observable collection store data objects listview. adding new objects collection works fine, , listview updates properly. when try change 1 of properties of object in collection listview not update properly. example, have observable collection datacollection. try
_datacollections.elementat(count).status = "active";
i perform change before long operation due button press. listview not reflect change. addmylistview.items.refresh()
;. works, listview not refreshed till button_click method complete, no then. example:
button1_click(...) { _datacollections.elementat(count).status = "active"; mylistview.items.refresh(); executelongoperation(); _datacollections.elementat(count).status = "finished"; mylistview.items.refresh(); }
the status never goto "active", go straight "finished" after method completes. tried using dispatcher this:
button1_click(...) { this.dispatcher.invoke(system.windows.threading.dispatcherpriority.background, (noargdelegate)delegate { _datacollection.elementat(count).status = "active"; mylistview.items.refresh(); }); executelongoperation(); this.dispatcher.invoke(system.windows.threading.dispatcherpriority.background, (noargdelegate)delegate { _datacollection.elementat(count).status = "finished"; mylistview.items.refresh(); }); }
however, not seem work correctly either. tips or ideas appreciated.
to solve created class called veryobservablecollection. each object add, hooks object's notifypropertychanged event handler triggers collectionchanged event. each object removed, removes handler. simple , give want. partial code:
public class veryobservablecollection<t> : observablecollection<t> /// <summary> /// override setting item /// </summary> /// <param name="index">index</param> /// <param name="item">item</param> protected override void setitem(int index, t item) { try { inotifypropertychanged propold = items[index] inotifypropertychanged; if (propold != null) propold.propertychanged -= new propertychangedeventhandler(affecting_propertychanged); } catch (exception ex) { exception ex2 = ex.innerexception; } inotifypropertychanged propnew = item inotifypropertychanged; if (propnew != null) propnew.propertychanged += new propertychangedeventhandler(affecting_propertychanged); base.setitem(index, item); }
Comments
Post a Comment