c# - WPF: Programatically adding a ContextMenu to a DataGrid column header -
i'm trying add individualized , separate contextmenus each column header in project, when user right-clicks header, menu of checkboxes relates header appear allows them filter data.
a couple of catches: project i'm working on needs developed .net 4.0, , such don't have access datagridcolumnheader
class introduced in .net 4.5. also, of needs done programatically, no xml allowed, of column data determined @ runtime.
i found similar stack question in done using xml, , i've sucessfully reproduced in xml, i'm new wpf , haven't been able reproduce programatically.
i've pasted c# code below think setup should occur.
/// <summary> /// function adds of columns default setup /// </summary> public void makeallcolumns() { (int = 0; < allcolumndisplaynames.length; i++) { datagridtextcolumn col = new datagridtextcolumn(); col.header = allcolumndisplaynames[i]; col.binding = new binding(allcolumnbindings[i]); cangrid.columns.add(col); // code addding context menus go here } }
datagridcolumns not frameworkelements not have contextmenu. since want in code behind specifying data template column can pain (imho). try passing in frameworkelement header object column , set context menu on framework element.
example:
//... datagridtextcolumn col = new datagridtextcolumn(); //... textblock txt = new textblock() { text = allcolumndisplaynames[i] }; txt.contextmenu = new contextmenu(); // build context menu here. col.header = txt; //...
Comments
Post a Comment