c# - Update bound control -


i'm learning wpf. figured out how bind list box using following code.

xaml

<listbox name="lstupdates" grid.row="1" grid.column="0" margin="5,0,5,5" itemssource="{binding historydata}" scrollviewer.horizontalscrollbarvisibility="disabled" loaded="lstupdates_loaded">     <listbox.itemtemplate>         <datatemplate>             <stackpanel >                 <textblock text="{binding header}" fontweight="bold" />                 <textblock text="{binding description}" textwrapping="wrap" height="46" />             </stackpanel>         </datatemplate>     </listbox.itemtemplate> </listbox> 

cs

public class historyrow {     public string header { get; set; }     public string description { get; set; }     public int articleupdateid { get; set; } }  public ienumerable<historyrow> historydata { get; set; }  datacontext = this; historydata = new list<historyrow> {     new historyrow { header = "10/29/1961", description = "blah blah" },     new historyrow { header = "12/2/1976", description = "blah blah" },     new historyrow { header = "5/24/1992", description = "blah blah" },     new historyrow { header = "2/18/2012", description = "blah blah" }, }; 

i've got works pretty well. question how can refresh listbox when data changes. i've found several "solutions" on web , none of them work me.

you need inform view changes of collection.
when using observablecollection source of listbox control, view listening event collectionchanges , update control when event raised.

so change type of historydata observablecollection<yourhistorydatatype>

to work need keep same instance of collection , update items.

to keep same instance create property on viewmodel:

private observablecollection<historyrow> _historydata;  public observablecollection<historyrow> historydata  {           {         return _historydata;     }     set      {          if(equals(_historydata, value) return;         _historydata = value;          this.onpropertychange(nameof(this.historydata);     } }   

calling onpropertychanged inform view instance of collection changed.

read this: implementing model-view-viewmodel pattern
there information observablecollection , propertychanged event


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -