datagridview - WPF edit autogenerated column header text -
i'm using wpf datagrid
display datatable
's. need able edit bound datatables (two-way binding).
i'm using datagrid followed:
<datagrid selectionunit="cellorrowheader" isreadonly="false" autogeneratecolumns="true" itemssource="{binding path=selecteditem.bindablecontent, fallbackvalue={x:null}}" />
the problem have, user can't edit columnheader
's cell content or rows. screenshot below illustrates porblem. thing can sort columns. there way edit column headers too, example when user clicks twice, or presses f2.
maybe style
' or headertemplate
job? have tried styles , control templates i've found around internet, without success.
edit:
i managed display column headers in textbox
(and not in textblock
) within autogeneratingtextcolumn event handler:
private void _editor_autogeneratingcolumn(object sender, datagridautogeneratingcolumneventargs e) { // first: create , add data template parent control datatemplate dt = new datatemplate(typeof(textbox)); e.column.headertemplate = dt; // second: create , add text box data template frameworkelementfactory txtelement = new frameworkelementfactory(typeof(textbox)); dt.visualtree = txtelement; // create binding binding bind = new binding(); bind.path = new propertypath("text"); bind.mode = bindingmode.twoway; // third: set binding in text box txtelement.setbinding(textbox.textproperty, bind); txtelement.setvalue(textbox.textproperty, e.column.header); }
but couldn't manage set binding correctly, if edit text in textboxes, not change text in column.header
-property (which auto-generated binding datatable
explained above).
you forgot set source of binding , mustn't set value after registration of binding. correct code following:
private void asdf_autogeneratingcolumn(object sender, datagridautogeneratingcolumneventargs e) { datatemplate dt = new datatemplate(typeof(textbox)); e.column.headertemplate = dt; frameworkelementfactory txtelement = new frameworkelementfactory(typeof(textbox)); dt.visualtree = txtelement; binding bind = new binding(); bind.path = new propertypath("header"); bind.mode = bindingmode.twoway; // set source here bind.source = e.column; txtelement.setbinding(textbox.textproperty, bind); // mustn't set value here, otherwise binding doesn't work // txtelement.setvalue(textbox.textproperty, e.column.header); }
additionally must change binding property header
, because adding binding text property of textbox.
Comments
Post a Comment