Disable selecting in WPF DataGrid

后端 未结 12 857
天命终不由人
天命终不由人 2020-12-14 05:30

How can I disable selecting in a WPFTooklit\'s DataGrid? I tried modifying the solution that works for ListView (from WPF ListView turn off selecti

12条回答
  •  星月不相逢
    2020-12-14 06:02

    If someone else is facing same problem, they may find it helpful.

    We had a requirement to disable few rows on datagrid, but at the same time allow ARROW key navigation on them. This is why we had to switch to 'IsHitTestVisible' instead of controlling 'IsEnabled' property. So we couldn't adopt to above solution of switching to 'IsEnable' property.

    Here is how I ended up solving this issue. I created a new attached property (RowEnable) for DataGridRow. This attached property can be bind to a viewmodel property to control 'virtual' enable and disablement. I also created a new style for DataGridCell where I am setting 'IsHitTestVisible' to false based on the same viewmodel property. So, consider it like a row which mouse/keyboard can see, but can't see its cells/columns. This means now I can style the row based on new attached property (RowEnabled) to look disabled/enabled. At the same time, I can view tooltips for these rows which are virtually disabled.

提交回复
热议问题