c# - How to bind datatable value in combobox which has static value? -
i have combobox few static values.
<combobox name="cmbboxfield" grid.column="4" grid.row="2" style="{staticresource comboboxstylefixedwidth}" itemssource="{binding}" ></combobox> mvvmmodle1.cmbboxfield.items.add(new customcomboboxitem("text box", "0")); mvvmmodle1.cmbboxfieldtype.items.add(new customcomboboxitem("pick list", "1")); mvvmmodle1.cmbboxfieldtype.items.add(new customcomboboxitem("check box", "2")); mvvmmodle1.cmbboxfieldtype.items.add(new customcomboboxitem("radio button", "3"));
when saving info in database table getting saved.
((customcomboboxitem)(mvvmmodle1.cmbboxfield.selectedvalue)).value.tostring();
now when trying edit form , binding value 1 time again combobox not showing value.
mvvmmodle1.cmbboxfield.selectedvalue = dtdatalist.rows[0]["controllist"].tostring().trim();
someone please help me in this. how bind selected value combobox?
there quite few problems code here:
you settingitemscontrol.itemssource
property default binding (bind current info context), wrong unless datacontext
type implements ienumerable
, isn't. if right because datacontext
is, example, observablecollection<t>
, still have issue because adding items statically combobox
instead of whatever itemssource
is. also, type of items adding customcomboboxitem
, i'm going assume inherits comboboxitem
. either way, can't selectedvalue
string since values in combobox
not strings. you should not have collection of customcomboboxitem
's, instead custom class in it's own viewmodel. now that's been said, here suggested solution problem:
<combobox itemssource="{binding path=mycollection}" selectedvalue="{binding path=myselectedstring}" selectedvaluepath="stringprop" /> public class customcomboboxitem : comboboxitem { // not sure property name is... public string stringprop { get; set; } ... } // i'm assuming don't have separate viewmodel class , you're using // actual window/page viewmodel (which shouldn't do...) public class mywpfwindow : window, inotifypropertychanged { public mywpfwindow() { mycollection = new observablecollection<customcomboboxitem>(); // add together values somewhere in code, doesn't have here... mycollection.add(new customcomboboxitem("text box", "0")); etc ... initializecomponent(); } public observablecollection<customcomboboxitem> mycollection { get; private set; } private string _myselectedstring; public string myselectedstring { { homecoming _myselectedstring; } set { if (string.equals(value, _myselectedstring)) return; _myselectedstring = value; raisepropertychanged("myselectedstring"); } } public void getstringfromdb() { // ... myselectedstring = dtdatalist.rows[0]["controllist"].tostring().trim(); } }
you alternatively not implement inotifypropertychanged , utilize dependencyproperty
myselectedstring property, using inpc preferred way. anyways, should give plenty info know direction head in...
tl;dr;
take advantage of bindingobservablecollection<t>
(create property this). add items (customcomboboxitem
s) observablecollection<t>
. bind itemssource
new collection property created. bind selectedvalue
string property create (take advantage of inpc). set selectedvaluepath
path of string property name of customcomboboxitem
. c# wpf binding combobox
No comments:
Post a Comment