How can I disable selecting in a WPFTooklit's DataGrid
? I tried modifying the solution that works for ListView
(from WPF ListView turn off selection), but that doesn't work:
<tk:DataGrid> <tk:DataGrid.ItemContainerStyle> <Style TargetType="{x:Type tk:DataGridRow}"> <Setter Property="Focusable" Value="false"/> </Style> </tk:DataGrid.ItemContainerStyle> <tk:DataGrid.CellStyle> <Style TargetType="{x:Type tk:DataGridCell}"> <Setter Property="Focusable" Value="false"/> </Style> </tk:DataGrid.CellStyle> </tk:DataGrid>
WPF DataGrid (SfDataGrid) allows you to select one or more rows or cells. For selecting specific row or group of rows you have to set SelectionUnit as Row and for selecting a specific cell or group of cells you have to set SelectionUnit as Cell or Any. In SelectionUnit.
If the value IsEnable = true, the Row on DataGrid is enabled. If IsEnable = false, the Row on the DataGrid is disabled.
The clean way would be, to just override the styles of the row and the cell
<DataGrid.Resources> <ResourceDictionary> <Style x:Key="{x:Type DataGridCell}" TargetType="{x:Type DataGridCell}"> <Setter Property="Background" Value="{x:Null}" /> <Setter Property="BorderBrush" Value="{x:Null}" /> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="{x:Null}" /> <Setter Property="BorderBrush" Value="{x:Null}" /> </Trigger> </Style.Triggers> </Style> <Style TargetType="{x:Type DataGridRow}"> <Setter Property="Background" Value="{x:Null}" /> <Setter Property="BorderBrush" Value="{x:Null}" /> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="{x:Null}" /> <Setter Property="BorderBrush" Value="{x:Null}" /> </Trigger> </Style.Triggers> </Style> </ResourceDictionary> </DataGrid.Resources>
To completely disable selection of rows in a DataGrid, you could do the following:
<DataGrid> <DataGrid.RowStyle> <Style TargetType="DataGridRow"> <Setter Property="IsHitTestVisible" Value="False"/> </Style> </DataGrid.RowStyle> <!--Other DataGrid items--> </DataGrid>
This could be considered more favorable than setting <Setter Property="IsEnabled" Value="False"/>
due to the fact that doing the aforementioned technique causes the style of the row to change. It also does not disable context menus from appearing when right-clicking.
Lastly: it is important to note that setting "IsHitTestVisible" to "False" disables all interaction with the rows, including editing.
However, if all you want to do is change the styling of the row when selected, please view the answers here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With