c# - Databound list box not updating to correct values from observable collection WPF -


i new wpf simple i've missed.

i have list box holding databound class properties static observablecollection<myclass>. collection being updated several times second network stream source , can tell debugging, collection being updated properly. declaration follows: static observablecollection<pumpitem> pumpcollection = new observablecollection<pumpitem>(); pumpitem name of class.

that not listbox isn't displaying however, updating display new values added collection these ever reflect properties of first moment enter collection.

the values of listbox bound such:

 <listbox x:name="pumplistbox" itemssource="{binding pumpcollection}" grid.issharedsizescope="true" margin="0,0,153,0">         <listbox.itemtemplate>             <datatemplate>                 <grid>                     <grid.columndefinitions>                         <columndefinition sharedsizegroup="id" />                         <columndefinition sharedsizegroup="state" />                         <columndefinition sharedsizegroup="selection" />                         <columndefinition sharedsizegroup="fuel pumped" />                         <columndefinition sharedsizegroup="cost" />                     </grid.columndefinitions>                     <textblock margin="2" text="{binding pumpid}" grid.column="0"/>                     <textblock margin="2" text="{binding state}" grid.column="1"/>                     <textblock margin="2" text="{binding selection}" grid.column="2"/>                     <textblock margin="2" text="{binding fuelpumped}" grid.column="3"/>                     <textblock margin="2" text="{binding fuelcost}" grid.column="4"/>                 </grid>             </datatemplate>         </listbox.itemtemplate>     </listbox> 

i have declaration in code behind in order set resources collection:

public static observablecollection<pumpitem> pumpcollection         {             { return pumpcollection; }          } 

in `mainwindow() constructor i've set:

this.datacontext = this; 

before initialisecomponent(); , background worker thread receive network inputs update list:worker.runworkerasync();`

this background thread loops continuously updates collection stream , invokes resource update:

 private void worker_dowork(object sender, doworkeventargs e)     {         //background tasks         thread.sleep(500); //allows ui time update , initialise         string s_decryptedmessage = string.empty;          app.current.dispatcher.invoke((action)(() =>         {             resources["pumpcollection"] = pumpcollection;          }));           while (true)         {              bytemessage = recieve(stream);//get number of pumps recieved             interact(bytemessage); //signal server update pumplist          } } 

if helps, class code thus:

namespace posclientwpf 

{ public enum pumpstate { available, waiting, pumping, paying };

public enum fuelselection {     petrol,     diesel,     lpg,     hydrogen,     none  }; public class pumpitem {     private string pumpid;     public string pumpid     {                 {             return pumpid;         }         set         {             pumpid = value;         }     }      private double fuelpumped;     public double fuelpumped     {                 {             return fuelpumped;         }         set         {             fuelpumped = value;         }     }      private double fuelcost;     public double fuelcost     {                 {              return fuelcost;         }         set         {              fuelcost = math.round(value, 2); //cost 2 dp         }     }      private fuelselection selection;     public fuelselection selection     {                 {             return selection;         }         set         {             selection = value;         }     }      private pumpstate state;     public pumpstate state     {                 {             return state;         }         set         {             state = value;         }     }      public pumpitem(string _id, pumpstate _state, fuelselection _selection)     {         this.pumpid = _id;         this.fuelpumped = 0;         this.fuelcost = 0;         this.selection = _selection;          this.state = _state;     } } 

}

as said, i'm new wpf appreciate guidance or solutions. thanks.

ash correct here quick example skim. , example of aviewmodelbase typically viewmodels inherit from. 1 of repos. how call onchangedevent.

private sample _item;     public sample item     {                 {             return _item;         }         set         {             if (_item != value)             {                 _item = value;                 onpropertychanged("item");             }         }     } 

this update when properties change.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -