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
All of the above are good ideas for easy hacks. However, they aren't doing exactly what is asked. The other answers are telling us how to unselect something selected by the user or hide the fact that something was selected by the user.
However, I understand why these answers are given. It is not easy to provide the real solution.
The real solution is to prevent selection in the first place, which it is not straightforward but it is doable with a few easy steps.
Answer 1. You have to copy the style in Expression Blend (or find a copy of the style somewhere). 2. Change a single ItemPresenter setting. It was enough for me to set IsHitTestVisible="False" on the ItemPresenter.
If you need more details, or an in-depth walk-thru for doing this, see my blog post:
How to disable row selection in a WPF DataGrid?
In case you are using alternate colors:
<Style TargetType="{x:Type DataGrid}">
<Setter Property="RowBackground" Value="#badeee"/>
<Setter Property="AlternationCount" Value="2" />
<Setter Property="AlternatingRowBackground" Value="#92cce5"/>
</Style>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="#badeee"></Setter>
<Setter Property="BorderBrush" Value="#badeee"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="#92cce5"></Setter>
<Setter Property="BorderBrush" Value="#92cce5"></Setter>
</Trigger>
</Style.Triggers>
</Style>
The only proper way I found to do this is to disable IsHitTestVisible property on DataGridRowStyle.
Click events will still register despite the naming. Make sure you do not change this property on the whole DataGrid unless you also want to disable scrolling.
A clean way of doing this is through a new Style in your static resources (copying other setters as appropriate)
<Style x:Key="DataGridUnselectableRowStyle" TargetType="{x:Type DataGridRow}">
<Setter Property="IsHitTestVisible" Value="False"/>
</Style>
and then binding it on your DataGrid
<DataGrid
RowStyle="{StaticResource DataGridUnselectableRowStyle}" >
<!-- Contents -->
</DataGrid>
There is a trick for this. You can handle SelectionChanged event of the DataGrid(say dgGrid) and in the handler write:
dgGrid.UnselectAll();
It will unselect all selected row and result will be "No row selected".
<DataGrid isEnabled="False">
</DataGrid>
This is the simplest way to directly answer your question: disable selecting in WPF Datagrid.
Simply add IsHitTestVisible="False"
to DataGrid
definition.