c# - Combobox binding breaks when dropped down -


i have combobox bound dependencyproperty on form. can add items property through button click , add them combobox. however, drop down combobox binding breaks. can still add items property, combobox never updates show them.

for example

  • click button 3 times
  • drop combobox down - have 3 items
  • click button 3 times
  • drop combobox down - still have 3 items

starting application again and:

  • click button 6 times
  • drop combobox down - have 6 items

xaml

<grid x:name="layoutroot"       background="white">     <combobox name="combtest"               itemssource="{binding users}"               width="50"               height="50"               horizontalalignment="left" />     <button click="buttonbase_onclick"             width="50"             height="50"             horizontalalignment="right" /> </grid> 

code behind

public mainpage() {     initializecomponent();     this.datacontext = this; }  public static readonly dependencyproperty usersproperty = dependencyproperty.register(  "users", typeof (list<string>), typeof (mainpage), new propertymetadata(new list<string>()));  public list<string> users {     { return (list<string>) getvalue(usersproperty); }     set { setvalue(usersproperty, value); } }  private int = 0;  private void buttonbase_onclick(object sender, routedeventargs e) {    users.add("user " + i++); } 

use observablecollection instead of list

observablecollection raise property change of adding , removing item in collection, list not.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -