ComboBox column in WPF DataGrid with DataTable as ItemsSource -
i've got datagrid bound datatable, comboboxcolumn. xaml column follows:
<datagridcomboboxcolumn header="rep name" sortmemberpath="repname" itemssource="{binding updatesourcetrigger=propertychanged, source={staticresource employeelist}, path=employees}" selectedvaluebinding="{binding mode=twoway, path=empid}" selectedvaluepath="empid" displaymemberpath="repname" />
my employees class:
public class employeelist : inotifypropertychanged { private observablecollection<employee> _employees = new observablecollection<employee>(); public employeelist() { ... } public observablecollection<employee> employees { { return _employees; } set { _employees = value; notifypropertychanged("employees"); } } public event propertychangedeventhandler propertychanged; public void notifypropertychanged(string propertyname) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propertyname)); } } } public class employee : inotifypropertychanged { private int _id; public int empid { { return _id; } set { _id = value; onpropertychanged("empid"); } } public string repname { get; set; } public event propertychangedeventhandler propertychanged; private void onpropertychanged(string propertyname) { if (propertychanged != null) propertychanged(this, new system.componentmodel.propertychangedeventargs(propertyname)); } }
the datatable serving items source grid contains "empid" column , "repname" column. combobox populated employees, , when make selection, reflected in datatable. however, when screen loads, assigned employee not selected default in combobox. thought selectedvaluebinding property of combobox handle this... doing wrong?
update clarification:
the datagrid bound datatable includes employeeid column. let's assume when screen loads, there 3 rows in table employeeids 1, 2, , 3. need combobox column in first row have employeeid 1 selected, second row have employeeid 2 selected, , third row have employeeid 3 selected.
if understand problem correctly, combobox
doesn't select first item in collection default?
if that's case, should set property issychronizedwithcurrentitem
on combobox
true. info selector.issynchronizedwithcurrentitem
here.
...
well, unfortunately upon further research, datagridcomboboxcolumn
not have issynchronizedwithcurrentitem
property. :/ but, can create datagridtemplatecolunm
looks this:
<datagridtemplatecolumn header="rep name"> <datagridtemplatecolumn.celltemplate> <datatemplate> <combobox itemssource="{binding employees}" issynchronizedwithcurrentitem="true" /> </datatemplate> </datagridtemplatecolumn.celltemplate> </datagridtemplatecolunm>
hope helps!
Comments
Post a Comment