How to suppress DataGrid cell selection border?

后端 未结 2 756
无人共我
无人共我 2021-01-03 08:40

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 borde

相关标签:
2条回答
  • 2021-01-03 08:58

    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.

    0 讨论(0)
  • 2021-01-03 09:06

    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

    0 讨论(0)
提交回复
热议问题