Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable DataGrid current cell border in FullRow selection mode

I am using a DataGrid in row selection mode (i.e., SelectionUnit="FullRow"). I simply want to remove the border that is being placed around the current cell when the user highlights a row in order to have true full row selection (and no cell level selection). I don't mind the notion of the grid maintaining the current cell, I just want to remove that pesky current cell border, perhaps by changing the style of the current cell. What is the easiest way to do this?

like image 820
Michael Goldshteyn Avatar asked Dec 28 '10 16:12

Michael Goldshteyn


2 Answers

You could set the BorderThickness for DataGridCell to 0

<DataGrid ...           SelectionUnit="FullRow">     <DataGrid.CellStyle>         <Style TargetType="DataGridCell">             <Setter Property="BorderThickness" Value="0"/>             <!-- Update from comments.                  Remove the focus indication for the selected cell -->             <Setter Property="FocusVisualStyle" Value="{x:Null}"/>         </Style>     </DataGrid.CellStyle>     <!-- ... --> </DataGrid> 
like image 122
Fredrik Hedblad Avatar answered Sep 18 '22 04:09

Fredrik Hedblad


Saw another answer here that was close, but it didn't get rid of the Focus rectangle. Here's how to obliterate all of the borders.

<DataGrid.Resources>     <Style TargetType="{x:Type DataGridCell}">         <Setter Property="BorderThickness" Value="0" />         <Setter Property="FocusVisualStyle" Value="{x:Null}" />     </Style> </DataGrid.Resources> 

Also, since technically those cells still do get focus (you just don't see it), to make the tab key advance to the next row instead of the next cell, I define a cell style based on the above but which also adds the following...

<DataGrid.Resources>     <Style x:Key="NoFocusDataGridCell" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">         <Setter Property="Focusable"        Value="False" />         <Setter Property="IsTabStop"        Value="False" />         <Setter Property="IsHitTestVisible" Value="False" />     </Style> </DataGrid.Resources> 

...then I apply that to all but the first column definition. That way the tab key advances to the next row, not the next cell.

Back to the borders however. If you want to hide them but still want them to be part of the layout for spacing-reasons, change the above to this...

<DataGrid.Resources>     <Style TargetType="{x:Type DataGridCell}">         <Setter Property="BorderBrush" Value="Transparent" />         <Setter Property="FocusVisualStyle" Value="{x:Null}" />     </Style> </DataGrid.Resources> 

Enjoy! :)

like image 40
Mark A. Donohoe Avatar answered Sep 18 '22 04:09

Mark A. Donohoe