c# - WPF Datagrid Superheader -
i using basic wpf datagrid. need add superheader. each superheader has span of 2 columns. also, has dynamically created because number of columns determined based upon data in database. have got this: superheader. problem is, if resize columns, superheader not moving expected. below code: mainwindow.xaml:
<tabitem header="tab4"> <stackpanel> <grid name="grdsuperheader"/> <datagrid name="grdsample" scrollviewer.horizontalscrollbarvisibility="disabled" autogeneratecolumns="false"> <datagrid.columns> <datagridtextcolumn x:name="samplecolumn1" header="column 1"></datagridtextcolumn> <datagridtextcolumn x:name="samplecolumn2" header="column 2"></datagridtextcolumn> <datagridtextcolumn x:name="samplecolumn3" header="column 3" ></datagridtextcolumn> <datagridtextcolumn x:name="samplecolumn4" header="column 4"></datagridtextcolumn> <datagridtextcolumn x:name="samplecolumn5" header="column 5"></datagridtextcolumn> <datagridtextcolumn x:name="samplecolumn6" header="column 6"></datagridtextcolumn> </datagrid.columns> <textblock></textblock> <textblock></textblock> <textblock></textblock> <textblock></textblock> <textblock></textblock> </datagrid> <stackpanel horizontalalignment="left" orientation="horizontal"> <textblock width="{binding elementname=samplecolumn1,path=actualwidth}" text="footer1" margin="5,0,0,0"></textblock> <textblock width="{binding elementname=samplecolumn2,path=actualwidth}" text="footer2" margin="5,0,0,0"></textblock> <textblock width="{binding elementname=samplecolumn3,path=actualwidth}" text="footer3" margin="5,0,0,0"></textblock> <textblock width="{binding elementname=samplecolumn4,path=actualwidth}" text="footer4" margin="5,0,0,0"></textblock> <textblock width="{binding elementname=samplecolumn5,path=actualwidth}" text="footer5" margin="5,0,0,0"></textblock> <textblock width="{binding elementname=samplecolumn6,path=actualwidth}" text="footer6" margin="5,0,0,0"></textblock> </stackpanel> </stackpanel> </tabitem>
mainwindow.xaml.cs:
private void addsuperheader() { //create columns (int i= 0;i< grdsample.columns.count;i++) { columndefinition col = new columndefinition(); grdsuperheader.columndefinitions.add(col); var binding = new binding("actualwidth") { elementname = "samplecolumn" + i.tostring() }; col.setbinding(columndefinition.widthproperty, binding); if(i%2==0) { //add header text textblock txtheader = new textblock(); txtheader.text = "cols["+(i+1).tostring()+"-"+(i+2).tostring()+"]"; txtheader.foreground = new solidcolorbrush(colors.green); txtheader.horizontalalignment = horizontalalignment.center; grid.setrow(txtheader, 0); grid.setcolumn(txtheader, i); grid.setcolumnspan(txtheader, 2); grdsuperheader.children.add(txtheader); } } }
what doing wrong?
i think thats problem gridsplitters
, binding
of actualwidth
width
. can use following workaround binding
.
columndefinition col = new columndefinition {width = gridlength.auto}; grdsuperheader.columndefinitions.add(col); var binding = new binding("actualwidth") { elementname = "samplecolumn" + i.tostring() }; col.setbinding(columndefinition.maxwidthproperty, binding); col.setbinding(columndefinition.minwidthproperty, binding);
bind actalwidth
minwidth
, maxwidth
, set columndefinition width="auto"
. worked in example.
Comments
Post a Comment