c# - Add comboBox items from code behind. [WPF] -
i grabbed code msdn. im trying similar, use list instead of 3 different strings. say
list<string> strlist = new list<string>(); strlist.add("created c#"); strlist.add("item 2"); strlist.add("item 3"); //msdn code below cbox = new combobox(); cbox.background = brushes.lightblue; cboxitem = new comboboxitem(); cboxitem.content = "created c#"; cbox.items.add(cboxitem); cboxitem2 = new comboboxitem(); cboxitem2.content = "item 2"; cbox.items.add(cboxitem2); cboxitem3 = new comboboxitem(); cboxitem3.content = "item 3"; cbox.items.add(cboxitem3); cv2.children.add(cbox);
tried cbox.items.add(strlist); tried forloop loop through each element, doesn't work either. ideas how can achieve this?
xaml:
<grid x:name="grid44" datacontext="{staticresource tblpermitsviewsource}" horizontalalignment="left" verticalalignment="top" width="409"> <grid.columndefinitions> <columndefinition width="auto"/> <columndefinition width="auto"/> </grid.columndefinitions> <grid.rowdefinitions> <rowdefinition height="auto"/> </grid.rowdefinitions> <label content="spr packet assigment" grid.column="0" horizontalalignment="left" margin="3" grid.row="0" verticalalignment="center" fontweight="bold"/> <combobox x:name="spr_assignedcombobox" grid.column="1" displaymemberpath="spr_assigned" horizontalalignment="left" height="auto" text="{binding spr_assigned}" itemssource="{binding items}" margin="3,5,-114.35,5" grid.row="0" verticalalignment="center" width="238.35" background="white" isreadonly="true" iseditable="true" > </combobox> </grid>
set items programmatically:
code-behind:
private void populatecombobox() { cbox.itemssource = new list<string> { "item1", "item2", "item3"}; }
xaml:
<combobox width="200" height="30" x:name="cbox" />
bind collection of items:
public class dummyclass { public int value { get; set; } public string displayvalue { get; set;} } public observablecollection<dummyclass> dummyclasscollection { { return new observablecollection<dummyclass> { new dummyclass{displayvalue = "item1", value = 1}, new dummyclass{displayvalue = "item2", value = 2}, new dummyclass{displayvalue = "item3", value = 3}, new dummyclass{displayvalue = "item4", value = 4}, }; } }
xaml:
<combobox width="200" height="30" x:name="cbox" itemssource="{binding dummyclasscollection}" displaymemberpath="displayvalue" />
Comments
Post a Comment