开发者

How to suppress DataGrid cell selection border?

开发者 https://www.devze.com 2023-02-20 23:34 出处:网络
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

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 s开发者_如何学运维uggestions?


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


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

精彩评论

暂无评论...
验证码 换一张
取 消