Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress DataGrid cell selection border?

Tags:

.net

wpf

xaml

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?

like image 319
user626528 Avatar asked Nov 30 '22 08:11

user626528


2 Answers

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

like image 131
David Avatar answered Dec 05 '22 10:12

David


You can set Focusable to False.

<DataGrid ...
      SelectionUnit="FullRow">
   <DataGrid.CellStyle>
      <Style TargetType="DataGridCell">
         <Setter Property="BorderThickness" Value="0"/>
         <Setter Property="Focusable" Value="False"/>
      </Style>
   </DataGrid.CellStyle>
   <!-- ... -->
</DataGrid>

Note that if you make DataGridCell.Focusable false then navigation in the datagrid with up/down arrow keys won't work.

like image 21
Greg Sansom Avatar answered Dec 05 '22 09:12

Greg Sansom