c# - WPF DataGridComboBoxColumn dynamic binding of Enum -


i have datagrid set item source list<user>. 1 of properties of user department enum descriptions.

in datagrid departments displayed combobox user select one. need bind enum value , description datagridcomboboxcolumn.

so far managed either bind enum datagridcomboboxcolumn.itemssource works description isn't taken account. or set collection of value, description datagridcomboboxcolumn.itemssource , set displaymemberpath, selectedvaluepath. in case value doesn't bind datagridcomboboxcolumn.

the view:

 <datagrid x:name="userdata" horizontalalignment="stretch" margin="10,157,10,80" verticalalignment="stretch" height="auto" width="auto"  autogeneratingcolumn="userdata_onautogeneratingcolumn" displaymemberpath="description"/> 

the code:

    private void userdata_onautogeneratingcolumn(object sender, datagridautogeneratingcolumneventargs e)     {         if (e.column.sortmemberpath == "department")         {             (e.column datagridcomboboxcolumn).itemssource = enumextension.providevalue();              (e.column datagridcomboboxcolumn).displaymemberpath = "description";             (e.column datagridcomboboxcolumn).selectedvaluebinding = new binding("value");             (e.column datagridcomboboxcolumn).selectedvaluepath = "value";         }     } 

enum extension:

public static class enumextension {     public static list<valuedescriptionvm<departments>> providevalue()     {         return enum.getvalues(typeof(departments))         .cast<object>()         .select(enumvalue => new valuedescriptionvm<departments>()         {             value = (departments)enumvalue,             description = getdescription((enum)enumvalue)         }).tolist();     }      private static string getdescription(enum enumvalue)     {         fieldinfo fi = enumvalue.gettype().getfield(enumvalue.tostring());         var attributes = (descriptionattribute[])fi.getcustomattributes(typeof(descriptionattribute), false);          if (attributes.length > 0)         {             return attributes[0].description;         }          return enumvalue.tostring();     } } 

the path of selectedvaluebinding should name of property of user class:

(e.column datagridcomboboxcolumn).selectedvaluebinding = new binding("department"); 

then binding should work provided type of department property of user class , value property of valuedescriptionvm<departments> class same.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -