wpf - How to update observable collection on property change and also store it on every change? -


i have dynamic listbox contains textbox display list items , can edit listbox item. application setting file contains string collection want bind listbox. want update setting files on every change of listbox item, created class implements inotifyproprtychanged. have converted string collection settings file observable collection of custom type has string property. bind textbox property of custom class , update source property on property change. want update observable collection well. , updates app setting file well. please me on this. appreciated. code:

public class windowviewmodel : inotifypropertychanged {      private observablecollection<urlmodel> customcollection;      public observablecollection<urlmodel> customcollection     {         { return customcollection; }         set         {             customcollection = value;             notifypropertychanged("customcollection");         }     }     public event propertychangedeventhandler propertychanged;     protected void notifypropertychanged(string property)     {         if (propertychanged != null)             propertychanged(this, new propertychangedeventargs(property));     }      public windowviewmodel()     {         list<string> customlist = properties.settings.default.customlist.cast<string>().tolist();         list<urlmodel> urllist = new list<urlmodel>();         urllist = customlist.select(item => new urlmodel() { urlstring = item }).tolist();         customcollection = new observablecollection<urlmodel>(urllist);     } }  public class urlmodel : inotifypropertychanged {      private string url;      public string urlstring     {         { return url; }         set         {             url = value;             notifypropertychanged("urlstring");          }      }      public event propertychangedeventhandler propertychanged;      protected void notifypropertychanged(string property)     {         if (propertychanged != null)             propertychanged(this, new propertychangedeventargs(property));      } }   /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window {     public mainwindow()     {         initializecomponent();          viewmodel = new windowviewmodel();         listtwo.itemssource = viewmodel.customcollection;     }      private windowviewmodel viewmodel;      public windowviewmodel viewmodel     {         { return viewmodel; }         set{             viewmodel = value;             datacontext = value;         }     } }    

}

add property changed event handler each url when inserting observablecollection

public windowviewmodel() {     list<string> customlist = properties.settings.default.customlist.cast<string>().tolist();     list<urlmodel> urllist = new list<urlmodel>();     urllist = customlist.select(item => new urlmodel() { urlstring = item }).tolist();     customcollection = new observablecollection<urlmodel>(urllist);      foreach(var model in customcollection)     {         model.propertychaged += settingsupdater; //settings update fucntion     } } 

a naive implementation of settingsupdater update whole list of urls in settings whenever 1 of them changes.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -