Access Combobox selectedText in WPF Datagrid column header? -
i have wpf datagrid
pretty standard, except headers not text, combobox
control.
here screenshot demonstrate:
goes without saying there multiple columns. columns created programatically.
what traverse column headers, , selectedtext each combobox.
how done?
edit:
the code expect writing along these lines:
foreach (datagridcolumnheader columnheader in columnheaders) { var combobox = (combobox) columnheader.controls[0]; var thedroidiamlookingfor = combobox.text; }
i don´t know how combobox in columnheader assume via columnheaderstyle, approach binding selectedvalueproperty property in codebehind or viewmodel
<combobox x:name="headercombobox" itemssource="{binding path=headersource}"> <combobox.selectedvalue> <binding path="headerselectedvalue" mode="twoway" /> </combobox.selectedvalue>
edit: c# solution (not tested)
foreach (var column in mygrid.columns) { var colheader = (dependencyobject)column.header; var headercombobox = colheader.getchildoftype<combobox>(); var selected = headercombobox.selectedvalue.tostring(); }
and here extensionmethod getchildoftype
public static t getchildoftype<t>(this dependencyobject depobj) t : dependencyobject { if (depobj == null) return null; (int = 0; < visualtreehelper.getchildrencount(depobj); i++) { var child = visualtreehelper.getchild(depobj, i); var result = (child t) ?? getchildoftype<t>(child); if (result != null) return result; } return null; }
edit 2: if define columnheader this
<datagridtextcolumn width="250"> <datagridtextcolumn.header> <combobox width="200"> <combobox.items> <comboboxitem content="test1"/> <comboboxitem content="test2"/> </combobox.items> </combobox> </datagridtextcolumn.header> </datagridtextcolumn>
then column.header combobox no need going down visualtree cast combobox
var colheader = (combobox)column.header;
maybe show me how define comboboxes in columnheader
Comments
Post a Comment