.net – How to suppress DataGrid cell selection border

netwpfxaml

I've tried setting border style as suggested here Disable DataGrid current cell border in FullRow selection mode, but it doesn't do the thing fully. Is disables cell border selection when you select using a mouse, but there is still a dashed cell border when making selection using keyboard. Any suggestions?

Best Answer

the dashed box you see is the cell's FocusedVisualStyle

you need to override it so that it is blank.

2 options here (one of them has to be the right one but as I didn't have time to try, I don't know which)

  • the visualStyle is set on the cell directly

this means you have to set it through the CellStyle property:

<DataGrid.CellStyle>
   <Style TargetType="DataGridCell">
      <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
   </Style>
</DataGrid.CellStyle>

or if you want to comply with MS's templating guidelines:

<DataGrid.Resources>

    <!--CellFocusVisual-->
    <Style x:Key="CellFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Border>
                        <Rectangle StrokeThickness="0" Stroke="#00000000" StrokeDashArray="1 2"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</DataGrid.Resources>

<DataGrid.CellStyle>
   <Style TargetType="DataGridCell">
      <Setter Property="FocusVisualStyle" Value="{StaticResource CellFocusVisual}"/>
   </Style>
</DataGrid.CellStyle>

(this way, you can see how it is done)

  • other option: it is done via the ElementStyle or the EditingElementStyle

this is more of a hasle there, because the ElementStyle and EditingElementStyle are defined on the Column, wich means you have to edit each column's ElementStyle and EditingElementStyle.

but basically, this is the same thing: you set up the FocusVisualStyle to null or the style defined above through the ElementStyle and/or EditingElementStyle on each Column