c# - WPF bind control width to ListBox column width -
i wrote xaml follows:
<usercontrol.resources> <gridviewcolumncollection x:key="gvcc"> <gridviewcolumn header="klient" displaymemberbinding="{binding customername}" width="80"> </gridviewcolumn> <gridviewcolumn header="budżet" displaymemberbinding="{binding budget, stringformat={}{0:c}}" width="80"> </gridviewcolumn> <gridviewcolumn header="zysk. zakł." displaymemberbinding="{binding expectedprofability, stringformat={}{0:c}}" width="80"> </gridviewcolumn> <gridviewcolumn header="zysk. real." displaymemberbinding="{binding realprofability, stringformat={}{0:c}}" width="80"> </gridviewcolumn> <gridviewcolumn header="miejsce" displaymemberbinding="{binding place}" width="80" /> <gridviewcolumn header="nr proj." displaymemberbinding="{binding number}" width="80"> </gridviewcolumn> </gridviewcolumncollection> </usercontrol.resources> ... <grid> <scrollviewer scrollviewer.verticalscrollbarvisibility="visible"> <itemscontrol itemssource="{binding groupedprojects}"> <itemscontrol.itemtemplate> <datatemplate> <grid> <grid.rowdefinitions> <rowdefinition /> <rowdefinition /> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="70" /> <columndefinition width="*" /> </grid.columndefinitions> <border grid.column="0" grid.row="0" grid.rowspan="3" verticalalignment="stretch" horizontalalignment="stretch" background="red" > <textblock fontsize="13" text="{binding month}" textalignment="center" verticalalignment="center" horizontalalignment="center" /> </border> <gridviewheaderrowpresenter name="hrp" columns="{staticresource gvcc}" grid.column="1" grid.row="0" /> <listbox itemssource="{binding details}" grid.column="1" grid.row="1" scrollviewer.verticalscrollbarvisibility="disabled" gotfocus="listbox_gotfocus" previewmousewheel="listbox_previewmousewheel" selecteditem="{binding relativesource={relativesource findancestor, ancestortype={x:type usercontrol}}, path=datacontext.selectedproject}"> <listbox.itemtemplate> <datatemplate> <gridviewrowpresenter columns="{staticresource gvcc}" height="24" /> </datatemplate> </listbox.itemtemplate> </listbox> <!-- here problem --> </grid> </datatemplate> </itemscontrol.itemtemplate> <itemscontrol.itemspanel> <itemspaneltemplate> <stackpanel orientation="vertical"/> </itemspaneltemplate> </itemscontrol.itemspanel> </itemscontrol> </scrollviewer> </grid>
data grouped in listbox, underneath listbox want display summary row. wanted row stackpanel textblock controls. problem - how relate width of textbox controls widths of columns in listbox.
let's "summary row" that:
<stackpanel orientation="horizontal"> <textblock width="bind listbox.column1.width + listbox.column2.width">some data</textblock> <textblock width="bind listbox.column3.width">other data</textblock> <textblock width="bind listbox.column4.width">etc.</textblock> </stackpanel>
as can see - first textblock width "columnspan = 2". width of other textblocks width of column pair them.
is such thing possible in xaml? can of knows how implement similar solution in different way?
if doing through mvvm, can use property binding done. set width of each of listbox columns properties in viewmodel. textbox controls widths bound seperate readonly property takes appropriate widths, manipulates them in way, , returns value. have implement inotifypropertychanged , call on each of textbox width properties every time listboxwidth property changed.
so so:
private _column1width double public property column1width() double return _column1width end set(byval value double) _column1width = value onpropertychanged("textbox1width") end set end property private _column2width double public property column2width() double return _column2width end set(byval value double) _column2width = value onpropertychanged("textbox1width") end set end property public readonly property textbox1width() double return column1width + (column2width * 2) end end property
and bindings so:
<gridviewcolumncollection x:key="gvcc"> <gridviewcolumn header="klient" displaymemberbinding="{binding customername}" width="{binding column1width, mode=twoway, updatesourcetrigger=propertychanged}"}"> </gridviewcolumn> <gridviewcolumn header="budżet" displaymemberbinding="{binding budget, stringformat={}{0:c}}" width="{binding column2width, mode=twoway, updatesourcetrigger=propertychanged}"> </gridviewcolumn> <gridviewcolumn header="zysk. zakł." displaymemberbinding="{binding expectedprofability, stringformat={}{0:c}}" width="80"> </gridviewcolumn> <gridviewcolumn header="zysk. real." displaymemberbinding="{binding realprofability, stringformat={}{0:c}}" width="80"> </gridviewcolumn> <gridviewcolumn header="miejsce" displaymemberbinding="{binding place}" width="80" /> <gridviewcolumn header="nr proj." displaymemberbinding="{binding number}" width="80"> </gridviewcolumn> </gridviewcolumncollection> <textblock width="{binding textbox1width}">some data</textblock>
everything resizes should continuously updating resize columns.
Comments
Post a Comment