c# - Listbox bound to an ObservableCollection doesn't update -


i using observablecollection store environment variables of windows.

class variableviewmodel {     observablecollection<variablemodel> vars;      public observablecollection<variablemodel> variables     {                 {             return vars;         }         set         {             vars = value;         }     }      public variableviewmodel()      {         reload();     }      public void reload()     {     // code retrieve vars     } } 

this observablecollection bound listbox.

i have added button in gui reload variables whichi, on click, calls reload() procedure.

listbox content, however, not change , cannot add anymore items list afer calling reload().

under constructor working fine.

listbox xaml :

<listbox x:name="lbvariables" grid.column="0" margin="0,0,5,0" itemssource="{binding source={staticresource variablesviewmodel}, path=variables}" issynchronizedwithcurrentitem="true">  

i tried using propertychanged updatesource trigger , set of modes.

public void reload()     {         vars = new observablecollection<variablemodel>();          registrykey systemvarkey = registry.localmachine.opensubkey(@"system\currentcontrolset\control\session manager\environment");          string[] systemvars = systemvarkey.getvaluenames();          foreach (string var in systemvars)         {             vars.add(new variablemodel()             {                 name = var,                 values = (systemvarkey.getvalue(var, null, registryvalueoptions.donotexpandenvironmentnames) string).split(';').tolist<string>(),                 target = environmentvariabletarget.machine             });         }          systemvarkey.close();          registrykey uservarkey = registry.currentuser.opensubkey(@"environment");          string[] uservars = uservarkey.getvaluenames();          foreach (string var in uservars)         {             vars.add(new variablemodel()             {                 name = var,                 values = (uservarkey.getvalue(var, null, registryvalueoptions.donotexpandenvironmentnames) string).split(';').tolist<string>(),                 target = environmentvariabletarget.user             });         }         uservarkey.close();     } 

you need implement inotifypropertychanged variableviewmodel refresh target object bindings. have way -

class variableviewmodel : inotifypropertychanged     { .       .             public observablecollection<variablemodel> variables             {                               {                   return vars;                }                set                {                   if(vars!=value)                   {                       vars = value;                       onpropertychanged("variables");                   }                }             }              public event propertychangedeventhandler propertychanged;                   protected void onpropertychanged(string name)             {                 propertychangedeventhandler handler = propertychanged;                 if (handler != null)                 {                     handler(this, new propertychangedeventargs(name));                 }             }      } 

Comments