c# - WPF ObservableCollection not updating in view -
i working on project using mvvm user connects database server , presented list of database areas on server use. list of areas, using combobox has itemsource bound observablecollection.
combobox itemssource="{binding areacollecton}" selecteditem="{binding selectedarea}"
in viewmodel, have observablecollection defined this:
private observablecollection<string> areacollection = new observablecollection<string>(); public observablecollection<string> areacollection { { return areacollection; } set{ areacollection = value; onpropertychanged("areacollection"); } }
the collection updated when server name entered , connect button clicked. click event poll server entered areas available , return list viewmodel.
public void connect() { areacollection.clear(); serverconnection = new serverconnect(); arealist = serverconnection.getarealist(server); //arealist defined dictionary<string, string> foreach(keyvaluepair<string, string> area in arealist) { areacollection.add(area.key); } onpropertychanged("areacollection"); }
i put break point @ foreach in connect method (invoked button click event) , stepped through saw areacollection being updated arealist expected. however, once completed building areacollection, nothing showing in combobox on view.
i new wpf , mvvm have feeling there small missing here. i've looked through of other posts here relating similar issues, , have implemented suggestions there still not updating view not sure missing. appreciated.
edit: i'm adding full xaml in case more helpful combobox line.
<grid height="400" width="400" verticalalignment="center" horizontalalignment="center"> <grid.columndefinitions> <columndefinition width="*" /> <columndefinition width="1.5*" /> </grid.columndefinitions> <grid.rowdefinitions> <rowdefinition height="50" /> <rowdefinition height="30" /> <rowdefinition height="30" /> <rowdefinition height="30" /> <rowdefinition height="30" /> <rowdefinition height="30" /> <rowdefinition height="30" /> <rowdefinition height="*" /> </grid.rowdefinitions> <textblock grid.row="0" grid.columnspan="2" horizontalalignment="center" verticalalignment="center" x:name="lblprompt" textwrapping="wrap" text="enter laboratory login , connection info:" fontsize="18" /> <label style="{staticresource connectlabel}" grid.row="1" grid.column="0" content="server:" /> <textbox style="{staticresource connecttextbox}" grid.row="1" grid.column="1" x:name="txtbxservername" textwrapping="wrap" tabindex="3" text="{binding server}" /> <button style="{staticresource connectbutton}" grid.row="2" grid.column="1" x:name="btnconnect" content="connect" fontsize="14"> <i:interaction.triggers> <i:eventtrigger eventname="click"> <ei:callmethodaction targetobject="{binding}" methodname="connect" /> </i:eventtrigger> </i:interaction.triggers> </button> <label style="{staticresource connectlabel}" grid.row="3" grid.column="0" content="area:" /> <combobox style="{staticresource connectcombo}" grid.row="3" grid.column="1" x:name="txtbxareaname" tabindex="4" itemssource="{binding areacollecton}" selecteditem="{binding selectedarea}" > <i:interaction.triggers> <i:eventtrigger eventname="selectionchanged"> <ei:callmethodaction targetobject="{binding}" methodname="areaconnect" /> </i:eventtrigger> </i:interaction.triggers> </combobox> <label style="{staticresource connectlabel}" grid.row="4" grid.column="0" content="lab username:" /> <textbox style="{staticresource connecttextbox}" grid.row="4" grid.column="1" x:name="txtbxusername" textwrapping="wrap" tabindex="1" text="{binding username}" /> <label style="{staticresource connectlabel}" grid.row="5" grid.column="0" content="lab password:" /> <passwordbox margin="2" grid.row="5" grid.column="1" x:name="pwdbxpassword" tabindex="5" passwordchanged="passwordbox_passwordchanged" /> <button style="{staticresource connectbutton}" grid.row="6" grid.column="1" x:name="btnlogin" content="login" fontsize="14"> <i:interaction.triggers> <i:eventtrigger eventname="click"> <ei:callmethodaction targetobject="{binding}" methodname="login" /> </i:eventtrigger> </i:interaction.triggers> </button> <textblock grid.row="7" grid.columnspan="2" horizontalalignment="center" margin="10" textwrapping="wrap" fontsize="14" text="{binding status}" /> </grid>
at first glance, see combox itemsource name different setting in code behind. shouldn't "areacollection" instead of "areacollecton". let me know if solves issue.
Comments
Post a Comment