c# - resizing the last column of a datagrid -
i have datagrid used in 2 different views. in each case, have last column resize it's width if user resizes host control/view.
how that?
cheers,
berryl
... canuserresizecolumns="true" >
<datagrid.columns> <datagridtextcolumn header="number" binding="{binding businessid}" isreadonly="true" canusersort="true" canuserresize="false" width="75"/> <datagridtextcolumn header="description" binding="{binding description}" isreadonly="true" canusersort="true" sortdirection="ascending" canuserresize="true" minwidth="260" width="auto" /> </datagrid.columns> </datagrid>
update (working code)
i named column in xaml , put following code code-behind. if has got better idea or way optimize this, please let me know!
public partial class listing : usercontrol { private double _currentcolumnwidth; public listing() { initializecomponent(); loaded += onloaded; sizechanged += onsizechanged; } private void onloaded(object sender, routedeventargs e) { _currentcolumnwidth = coldescription.actualwidth; } private void onsizechanged(object sender, sizechangedeventargs e) { // split if control not loaded yet if (_currentcolumnwidth == 0) return; // interested in width, not height var widthchanged = e.widthchanged; if (!widthchanged) return; var delta = e.newsize.width - e.previoussize.width; var newwidth = _currentcolumnwidth + delta; if (newwidth <= coldescription.minwidth || newwidth >= coldescription.maxwidth) return; _currentcolumnwidth = newwidth; coldescription.width = new datagridlength(_currentcolumnwidth); } }
easy, replace width property in xaml
<datagridtextcolumn header="description" binding="{binding description}" isreadonly="true" canusersort="true" sortdirection="ascending" canuserresize="true" minwidth="260" width="auto" />
to ...
<datagridtextcolumn header="description" binding="{binding description}" isreadonly="true" canusersort="true" sortdirection="ascending" canuserresize="true" minwidth="260" width="*" />
you not require code behind handling of width wpf caters in xaml.
the "*" indicates autosize value :)
Comments
Post a Comment