c# - Why my second ObservableCollection is getting update? -
i have two observablecollections filled items (that items observablecollecions filled other types of object) of same type.
_treeviewbase - first observablecollection 'base' data.
_tvdata - second observablecollection should store 'needed' items, have treeview itemsource binded tvdata.
function fillaliastreeview used fill _treeviewbase data not paste code here.
when @ begining of program assign _treeviewbase _tvdata in loadsidebar() function ok. both of observablecollection storing data.
but when search ( , tvdata modified in searchbox_textchanged() ) it's seems _treeviewbase modified.
how possible? did made major mistake in code?
my class ( full code ) :
public partial class sidebar : page, inotifypropertychanged { private observablecollection<connectionmodel> _treeviewbase; private observablecollection<connectionmodel> _tvdata ; public sidebar() { treeviewbase = new observablecollection<connectionmodel>( connectionutilities.loadobservableconnections() ) ; tvdata = new observablecollection<connectionmodel>(); initializecomponent(); datacontext = this; var loadsidebar = task.factory.startnew( async () => { await loadsidebar(); } ); loadsidebar.wait(); onpropertychanged("tvdata"); } public async task loadsidebar() { // function used fill _treeviewbase await fillaliastreeview(); tvdata = treeviewbase; onpropertychanged("tvdata"); } private async void searchbox_textchanged(object sender, textchangedeventargs e) { string newsearchtext = searchbox.text; if ( (newsearchtext == "" || newsearchtext == "search..." || newsearchtext.length < 3 ) ) { tvdata.clear(); tvdata = null; messagebox.show( (tvdata == null).tostring() + " " + treeviewbase.count.tostring()); tvdata = new observablecollection<connectionmodel>( treeviewbase ); aliastree.itemssource = tvdata ; aliastree.updatelayout(); return; } searchtext = searchbox.text; if (tvdata != null) { await dispatcher.invokeasync( () => { foreach (connectionmodel cm in tvdata.tolist<connectionmodel>()) { foreach (schemamodel sm in cm.schemas.tolist<schemamodel>()) { foreach (schemacollection sc in sm.schema_collections.tolist<schemacollection>()) { try { observablecollection<tablemodel> octm = (observablecollection<tablemodel>)sc.collection; (int = 0; < octm.count; i++) { if (!(sc.collection observablecollection<tablemodel>)[i].table_name.contains(searchtext)) { (sc.collection observablecollection<tablemodel>).removeat(i); = - 1; } dispatcher.invokeasync( () => onpropertychanged("tables") ) ; } dispatcher.invokeasync(() => onpropertychanged("collection") ); } catch (exception exc) { } } dispatcher.invokeasync(() => onpropertychanged("schemas") ); } dispatcher.invokeasync(() => onpropertychanged("tvdata")); } dispatcher.invokeasync(() => onpropertychanged("tvdata")); }); await dispatcher.invokeasync(() => expandall(aliastree)); } } private void expandall( itemscontrol root ) { if ( root != null ) { foreach (var subitem in root.items) { if (subitem != null) { expandall( (treeviewitem)root.itemcontainergenerator.containerfromitem(subitem) ); try { if (((treeviewitem)root.itemcontainergenerator.containerfromitem(subitem) treeviewitem) != null ) ((treeviewitem)root.itemcontainergenerator.containerfromitem(subitem) treeviewitem).isexpanded = true; } catch (exception exc) { }; } } aliastree.updatelayout(); } }
thank suggestions.
in constructor, set tvdata
, treeviewbase
2 different instances of observablecollection<>
. line:
tvdata = treeviewbase;
makes them refer same instance. old tvdata
observable collection discarded, , both of variables refer same observable collection.
this how reference types work.
Comments
Post a Comment