WPF datagrid - enable selecting, disabling text input -


i have c# wpf datagrid, checkbox column, hyperlink columns , text columns. datagrid bound datatable. columns not auto generated, create them in code dynamically, since number of columns not known in advance. enable text in cells selected (for ctrl+c purpose) yet disable editing. don't want text changed. can help?

one possibility use datagridtemplatecolumn:

<datagridtemplatecolumn>     <datagridtemplatecolumn.celltemplate>         <datatemplate>             <textbox isreadonly="true" text="{binding yourproperty,mode=oneway}"/>                                     </datatemplate>                             </datagridtemplatecolumn.celltemplate>                     </datagridtemplatecolumn> 

this works checkboxes, add checkbox, bind ischecked , use content textbox set isreadonly.

<datagridtemplatecolumn>     <datagridtemplatecolumn.celltemplate>         <datatemplate>             <checkbox ischecked="{binding yourbooleanvalue}">                 <textbox isreadonly="true" text="yourcopyabletextorabindingtotext"/>             </checkbox>         </datatemplate>     </datagridtemplatecolumn.celltemplate> </datagridtemplatecolumn> 

if want have checkbox readonly, set enabled-property false. in case, have declare textbox not child sibling of checkbox (use grid or stackpanel) this.

if want make data readonly whole datagrid, use:

<datagrid isreadonly="true"> 

this possible columns:

<datagridtextcolumn isreadonly="true"> 

if want define per row, have use datagridtemplatecolumns , bind isreadonly-proeprty of edit-control.

<datagridtemplatecolumn>     <datagridtemplatecolumn.celltemplate>         <datatemplate>             <textbox isreadonly="{binind yourreadonlyproperty}" text="{binding yourproperty,mode=oneway}"/>         </datatemplate>                             </datagridtemplatecolumn.celltemplate>                     </datagridtemplatecolumn> 

Comments