c# wpf datagrid column width auto size add horizontal scroll if needed -


i'm working on project using wpf , have datagrid. in datagrid, have 6 columns , coded use , share width of datagrid. problem when 1 column has value long enough header width, long text cut text out , show can have base on width. want happen datagrid add horizontal scroll if text long, width of header increase , therefore since datagrid width divided headers, horizontal scroll appear. note other headers' width should not adjust or affected change instead horizontal scroll should added. below datagrid xaml code:

<datagrid x:name="accountsbalance_grd"                   horizontalalignment="left"                   margin="266,118,0,0"                   verticalalignment="top"                   height="498"                   width="836"                   arerowdetailsfrozen="true"                   autogeneratecolumns="false"                   canuserreordercolumns="false"                   canuserresizecolumns="false"                   canuserresizerows="false"                   canusersortcolumns="false"                   canuseraddrows="false"                   borderbrush="blue"                   background="white"                   foreground="blue"                   borderthickness="2"                   horizontalgridlinesbrush="black"                   loaded="accountsbalance_grd_loaded">             <datagrid.columnheaderstyle>                 <style targettype="datagridcolumnheader">                     <setter property="fontsize"                             value="20" />                 </style>             </datagrid.columnheaderstyle>             <datagrid.columns>                 <datagridtextcolumn header="name"                                     width="150"                                     binding="{binding accountbalancename}"                                     isreadonly="true" />                 <datagridtextcolumn header="total balance"                                     width="150"                                     binding="{binding accountbalancetotal}"                                     isreadonly="true" />                 <datagridtextcolumn header="amount"                                     width="120"                                     binding="{binding accountbalanceamount}"                                     isreadonly="true" />                 <datagridtextcolumn header="transaction"                                     width="160"                                     binding="{binding accountbalancetransaction}"                                     isreadonly="true" />                 <datagridtextcolumn header="date"                                     width="*"                                     binding="{binding accountbalancedate}"                                     isreadonly="true" />                 <datagridtextcolumn header="incharge"                                     width="*"                                     binding="{binding accountbalanceadmin}"                                     isreadonly="true" />             </datagrid.columns>             <datagrid.rowstyle>                 <style targettype="{x:type datagridrow}">                     <setter property="fontsize"                             value="15" />                     <setter property="fontfamily"                             value="arial" />                     <setter property="fontweight"                             value="bold" />                     <setter property="foreground"                             value="black" />                 </style>             </datagrid.rowstyle>         </datagrid> 

thanks in advance.

in place of datagridtextcolumn can use datagridtemplatecolumn follows:

<datagridtemplatecolumn header="transaction" width="160">     <datagridtemplatecolumn.celltemplate>         <datatemplate>             <scrollviewer horizontalscrollbarvisibility="auto" verticalscrollbarvisibility="disabled">                 <textblock text="{binding accountbalancetransaction}"></textblock>             </scrollviewer>         </datatemplate>     </datagridtemplatecolumn.celltemplate> </datagridtemplatecolumn> 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -