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?

- 71,784
- 24
- 131
- 181
-
I'd thought I'd wanted to this (and also avoid Tab/Shift-Tab key moves to next/prior cells as 2nd part of Mark A. Donohoe's A on Jun 4 '15 at 10:34 does), BUT I've since realized, that I still want right/left keys to move to the next/prior cells, when horizontal scrolling is enabled and active on the `DataGrid`, and when doing so, I still need the current cell borders. Donohoe's A simplified / fixed to *just* set “IsTabStop” = false on non-1st column does this (while still allowing row-select via click on any vs. just 1st column and next/prior row move via keyboard via Down/Up keys). – Tom Mar 02 '20 at 18:46
7 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>

- 83,499
- 23
- 264
- 266
-
2This doesn't work if the user uses the arrow keys. The dotted-line selection border still appears in the cells. – Michael Yanni Mar 11 '13 at 16:01
-
18@Michael Yanni: You are talking about the `FocusVisualStyle`. To disable it, set it to null in the `CellStyle` like `
` – Fredrik Hedblad Mar 11 '13 at 22:35 -
How can I get the missing spacing of the cells back with the border set to 0? – ygoe Feb 27 '14 at 14:11
-
1@LonelyPixel, Instead of setting the thickness to zero, set the border brush to transparent. – Mark A. Donohoe Sep 22 '15 at 14:22
-
4This doesn't actually stop those cells from getting focused. You just don't see it. The tab key will therefore look like it's not properly working as the focus cycles through the cells, not rows. I've added an answer which addresses this. – Mark A. Donohoe Sep 25 '15 at 00:35
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! :)

- 28,442
- 25
- 137
- 286
-
This is even closer but there's a caveat still: if you click on column 0 you get the active selection highlight brush (as expected), but clicking on column 1+ you get the inactive selection highlight brush (unexpected). The inactive brush sticks (clicking other rows) till you click column 0. Then the active brush sticks till the DataGrid loses focus. – Olson.dev Jul 01 '16 at 05:33
-
1Interesting. I wasn't seeing that before, but I will definitely try it now. The reason why I was adding that however was for fixing the tab order. That said, maybe the fix is as simple as changing it from focusable = false to istabstop = false. It should achieve the same thing. – Mark A. Donohoe Jul 01 '16 at 13:48
-
1I just tried the `IsTabStop="False"` tweak and it seems to cover all cases. Technically the non-first-column cell receives focus when you click and you can arrow left/right to change focus to other cells but tabbing goes to the next row's first cell or the next control (if one of the last row's cell's is focused). However, I doubt that's an issue. Maybe for screen readers? Anyway, thanks! – Olson.dev Jul 02 '16 at 10:01
-
1I'd thought I'd wanted what the OP wanted (and also avoid Tab/Shift-Tab key moves to next/prior cells as 2nd part of Mark A. Donohoe's A on Jun 4 '15 at 10:34 does), BUT I've since realized, that I still want right/left keys to move to the next/prior cells, when horizontal scrolling is enabled and active on the DataGrid, and when doing so, I still need the current cell borders. Donohoe's A simplified / fixed to just set “IsTabStop” = false on non-1st column does this (while still allowing row-select via click on any vs. just 1st column and next/prior row move via keyboard via Down/Up keys). – Tom Mar 02 '20 at 18:48
<Style x:Key="DataGrid" TargetType="DataGrid">
<Setter Property="CellStyle">
<Setter.Value>
<Style TargetType="DataGridCell">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Foreground" Value="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}" />
<Setter Property="Background" Value="{Binding Background, RelativeSource={RelativeSource TemplatedParent}}" />
</Style>
</Setter.Value>
</Setter>
</Style>

- 61
- 1
- 1
-
Adding those two lines helps keep the style applied by the ResourceDictionary, thanks @marius. – Rachel Sep 20 '12 at 08:09
The real answer is:
<!-- put it in your cell style -->
<DataTrigger Binding="{Binding SelectionUnit, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Value="FullRow">
<Setter Property="BorderThickness" Value="0" />
</DataTrigger>
The question was about disabling it only in FullRow selection mode but other answers provide a solution to disable it completely even in cell selection mode.
Answer to a very old question but believe it or not WPF and WinForms is still used nowadays.

- 6,385
- 12
- 53
- 96
In case you don't want to mess with XAML styles you could do this simple hack. It does not work as good as XAML styles but you can try it out and see if it fits you. It is fine for simple clicking on cells but if you try to drag cells this won't remove focus afterwards (although I am pretty sure you could add another case which checks for that).
private void YourDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
YourDataGrid.Focus();
}
PS: Don't forget to add the event handler to your DataGrid
's SelectionChanged
property.

- 380
- 3
- 11
If you want to show a border only when the cell is editable and selected you can override the DataGridCell template and add a multitrigger for when the cell IsSelected and not IsReadOnly. Then no border will be shown for cells if you set IsReadOnly = true for the column or DataGrid
<ControlTemplate x:Key="MellowDataGridCellTemplate" TargetType="{x:Type DataGridCell}">
<Grid>
<ContentPresenter VerticalAlignment="Center" />
<Rectangle Name="FocusVisual" Stroke="White" StrokeThickness="1" Fill="Transparent" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" IsHitTestVisible="false" Opacity="0" />
</Grid>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsReadOnly" Value="False" />
<Condition Property="IsSelected" Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="FocusVisual" Property="Opacity" Value="1"/>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
Use the template in a style
<Style TargetType="{x:Type DataGridCell}" x:Key="MellowGridDataGridCell">
<Setter Property="Template" Value="{StaticResource MellowDataGridCellTemplate}" />
</Style>
And use the style
<DataGrid CellStyle={StaticResource MellowGridDataGridCell >
...
</DataGrid>

- 1,104
- 2
- 15
- 26
If you're using the xceed
DataGridControl
then set the NavigationBehavior
to RowOnly
<xcdg:DataGridControl NavigationBehavior="RowOnly" SelectionMode="Single" ....

- 140,023
- 84
- 646
- 689