c# - WPF DataGrid with different UserControl in each Cell -
i hava data model looks this:
public class model { public string displayas {get;set;} // textbox, checkbox, combobox public string value {get;set;} public string displayname {get;set;} // row1, row2, ... }
now want display these models in datagrid shall this:
how achieve this? please provide example code. tried whole day different kind of datatemplateselectors can't working
your selector selects template cells in second column based on displayas
value. have add templates datagrid.resources
. in second column, assign celltemplateselector
public class dynamicdatatemplateselector: datatemplateselector { public override datatemplate selecttemplate(object item, dependencyobject container) { frameworkelement element = container frameworkelement; if (element != null && item != null && item task) { model model = item model; return element.findresource(model.displayas + "template"); } return null; } } <datagrid> <datagrid.resources> <datatemplate x:key="textboxtemplate"> <textbox text="{binding value}"/> </datatemplate> <datatemplate x:key="checkboxtemplate"> <checkbox ischecked="{binding value}"/> </datatemplate> <datatemplate x:key="comboboxtemplate"> <combobox selecteditem="{binding value}"/> </datatemplate> </datagrid.resources> <datagrid.columns> <datagridtemplatecolumn header="rowname"> <datagridtemplatecolumn.celltemplate> <datatemplate> <textblock text="{displayname}"/> </datatemplate> </datagridtemplatecolumn.celltemplate> </datagridtemplatecolumn> <datagridtemplatecolumn header="data" celltemplateselector="{staticresource dynamicdatatemplateselector}"/> <datagrid.columns> <datagrid/>
Comments
Post a Comment