c# - How do I display the first item in my ComboBox based on my dataBinding? -
xaml:
<combobox grid.row="1" name="cmbclasse" grid.column="3" margin="5,5,5,5" itemssource="{binding datacontext.classeses}" selectedvaluepath="classeid" displaymemberpath="classename" selectedvalue="{binding datacontext.currentselectedpersonagem.idclasse, mode=twoway}"/>
when open application, combobox not have display default item. item list working, have items on it. please help, if image needed ask. thanks.
here method use time.
firstly, view model should have 2 properties, 1 represent collection, , other represent selected item.
public ienumerable<yourobject> myobjects { get; private set; } public yourobject selectedobject { get; private set; }
note: idea use observablecollection
myobjects
, , implement inotifypropertychanged
. use only if need create brand new instances of collection in view model.
your combobox
should bind these properties, so:
<combobox itemssource="{binding myobjects}" selecteditem="{binding selectedobject}" ... />
now, here comes magic. when create collection, set selectedobject
firstordefault
item in list.
private void loaddata() { //todo: load data collection //set first item this.selectedobject = myobjects.firstordefault(); }
this ensure first item selected. do not need implement inotifypropertychanged
on selectedobject
property unless planning on manually changing value of property in view model.
Comments
Post a Comment