Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable selecting in WPF DataGrid

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> 
like image 343
svick Avatar asked Mar 23 '10 00:03

svick


People also ask

How to select row in DataGrid WPF?

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.

How to disable row selection in DataGrid WPF?

If the value IsEnable = true, the Row on DataGrid is enabled. If IsEnable = false, the Row on the DataGrid is disabled.


2 Answers

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> 
like image 78
Alex Avatar answered Sep 16 '22 16:09

Alex


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.

like image 42
JoshuaTheMiller Avatar answered Sep 19 '22 16:09

JoshuaTheMiller