c# - ListView not updating when the bound ObservableCollection changes -
i'm working on search window loads search results observablecollection , displays results using listview.
setting listview's itemsource observablecollection after search complete populates list correctly.
i'm trying listview update search adds additional results, listview doesn't populate data @ all. can't work out binding falling over.
my research showed various ways of using datacontext, though none seem help; i've tried assigning "this" , cacheddata class using codebehind @ xaml window level.
sorry long code snippets, i've left think may add context question.
xaml:
<window x:class="slx_interface.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:slx_interface" mc:ignorable="d" title="slx search" height="auto" width="auto"> <window.commandbindings> </window.commandbindings> <grid> <grid.resources> <local:cacheddata x:key="cacheddata" /> </grid.resources> <tabcontrol x:name="tabcontrol" grid.rowspan="2" margin="0,20,0,0"> <tabitem header="accounts" name="accountstab"> <grid> <listview x:name="accountsearchresultslistview" margin="5,32,5,30" datacontext="staticresource cacheddata" itemssource="{binding path=accounts}" issynchronizedwithcurrentitem="true"> <listview.view> <gridview x:name="accountsearchresultsgridview"> <gridviewcolumn header="sdata key" displaymemberbinding="{binding sdatakey}"/> <gridviewcolumn header="account name" displaymemberbinding="{binding accountname}"/> </gridview> </listview.view> </listview> </grid> </tabitem> </tabcontrol> </grid>
code-behind within mainwindow.xaml.cs:
private async void searchaccount(string searchterm, string searchfield, string searchoperator) { //create string we'll use searching string urlstring = "stuff"; //create observablecollection, use blank cache observablecollection<account> resultslist = new observablecollection<account>(); cacheddata.accounts = resultslist; //getting data search using xml reader xmlreader resultsreader = null; try { //using xmlreader grab search results slx xmlurlresolver resultsresolver = new xmlurlresolver(); resultsresolver.credentials = logincredentials.usercred; xmlreadersettings resultsreadersettings = new xmlreadersettings(); resultsreadersettings.xmlresolver = resultsresolver; resultsreadersettings.async = true; resultsreader = xmlreader.create(urlstring, resultsreadersettings); } catch (exception error) { } //grabbing data xml , storing it, updating listview go using (resultsreader) { while (await resultsreader.readasync()) { while (resultsreader.readtofollowing("slx:account")) { //setting data xml new account object ready passed list account account = new account(); account.guid = new guid(); resultsreader.movetofirstattribute(); account.sdatakey = resultsreader.value; resultsreader.readtofollowing("slx:accountname"); account.accountname = resultsreader.readelementcontentasstring(); cacheddata.accounts.add(account); //--uncommenting gives odd results; //--the first item displayed, others aren't. //--if there lot of items, application errors mad , ends. //--looks 1 error window each item, though don't see message before die along application. //accountsearchresultslistview.itemssource = cacheddata.accounts; } } } //--uncommenting works shows data once entire xml has been read through, can take time isn't ideal. //accountsearchresultslistview.itemssource = cacheddata.accounts; }
classes references above, stored in separate .cs file under same namespace:
public class cacheddata { public static observablecollection<account> accounts { get; set; } public static event propertychangedeventhandler propertychanged; public static event eventhandler<propertychangedeventargs> staticpropertychanged = delegate { }; private static void notifystaticpropertychanged(string propertyname) { staticpropertychanged(null, new propertychangedeventargs(propertyname)); } } public class account : iequatable<account> { public guid guid { get; set; } public string sdatakey { get; set; } public string accountname { get; set; } public override string tostring() { return accountname; } public override bool equals(object obj) { if (obj == null) return false; account objaspart = obj account; if (objaspart == null) return false; else return equals(objaspart); } public override int gethashcode() { return 0; } public bool equals(account other) { if (other == null) return false; return (guid.equals(other.guid)); } }
i appreciate can offer, has stumped me days now.
when set data binding in xaml, instance of observablecollection exists when app starts bound. instantiate instance before app starts , not replace new instance unless reset data binding in code behind. if need clear elements, use clear method.
Comments
Post a Comment