c# - How set an Enum value as selectedItem in a combobox in wpf -


ok have found lot of question here of them seems old , complicated me.

i have combobox binded viewmodel class.

class settingsviewmodel {     public picturerecognitionintensivity picturerecognitionintensivity { get; set; }      public array picturerecognitionintensivityvalues     {         { return picturerecognitionintensivity.getvalues(typeof(picturerecognitionintensivity)); }     }      public settingsviewmodel()     {         // set default values testing;         this.picturerecognitionintensivity = picturerecognitionintensivity.moderate;     }   } 

the xaml part:

<combobox selecteditem="{binding path=picturerecognitionintensivity}" itemssource="{binding path=getpicturerecognitionintensivityvalues}" /> 

this code load values combobox , when selection changed value change selected item works except it doesnt load initial state combobox. (it empty @ beginnings.)

somewhere saw enum.getvalues gives strings tried convert string this:

return this.picturerecognitionintensivity.tostring(); 

but didn't work either. question simple , easy way (i cant believe still complicated in 2016 have write 20 lines of code.) add enum instance combobox , set selecteditem it.

the simplest way suggest given enum

public enum picturerecognitionintensivity {     first,     second,     third,     forth, }  

declare static instance in view resource below

    <window.resources>       <objectdataprovider x:key="datafromenum" methodname="getvalues" objecttype="{x:type system:enum}">          <objectdataprovider.methodparameters>             <x:type typename="local:picturerecognitionintensivity"/>          </objectdataprovider.methodparameters>       </objectdataprovider>    </window.resources> 

then in combobox bind follows:

<combobox itemssource="{binding source={staticresource datafromenum}}" selecteditem="{binding intensity }"/> 

then setting initial enum value should work expected.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

json - Gson().fromJson(jsonResult, Myobject.class) return values in 0's -